leet-code/src/main/java/leetcode/editor/cn/Base7.java
2022-03-07 13:54:55 +08:00

57 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给定一个整数 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)
}