504:七进制数
This commit is contained in:
parent
9e31ec07e4
commit
5256782e58
56
src/main/java/leetcode/editor/cn/Base7.java
Normal file
56
src/main/java/leetcode/editor/cn/Base7.java
Normal 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)
|
||||
|
||||
}
|
26
src/main/java/leetcode/editor/cn/doc/content/Base7.md
Normal file
26
src/main/java/leetcode/editor/cn/doc/content/Base7.md
Normal file
@ -0,0 +1,26 @@
|
||||
<p>给定一个整数 <code>num</code>,将其转化为 <strong>7 进制</strong>,并以字符串形式输出。</p>
|
||||
|
||||
<p> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-10<sup>7</sup> <= num <= 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>
|
Loading…
Reference in New Issue
Block a user