504:七进制数

This commit is contained in:
轩辕龙儿 2022-03-07 13:54:55 +08:00
parent 9e31ec07e4
commit 5256782e58
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,56 @@
//给定一个整数 num将其转化为 7 进制并以字符串形式输出
//
//
//
// 示例 1:
//
//
//输入: num = 100
//输出: "202"
//
//
// 示例 2:
//
//
//输入: num = -7
//输出: "-10"
//
//
//
//
// 提示
//
//
// -10 <= num <= 10
//
// Related Topics 数学 👍 148 👎 0
package leetcode.editor.cn;
//504:七进制数
public class Base7 {
public static void main(String[] args) {
Solution solution = new Base7().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String convertToBase7(int num) {
boolean bl = num < 0;
num = Math.abs(num);
StringBuilder str = new StringBuilder();
while (num >= 7) {
str.insert(0, num % 7);
num /= 7;
}
str.insert(0, num);
if (bl) {
str.insert(0, '-');
}
return str.toString();
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,26 @@
<p>给定一个整数 <code>num</code>,将其转化为 <strong>7 进制</strong>,并以字符串形式输出。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> num = 100
<strong>输出:</strong> "202"
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> num = -7
<strong>输出:</strong> "-10"
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>-10<sup>7</sup>&nbsp;&lt;= num &lt;= 10<sup>7</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>数学</li></div></div><br><div><li>👍 148</li><li>👎 0</li></div>