给定一个整数 num,将其转化为 7 进制,并以字符串形式输出。
示例 1:
输入: num = 100
输出: “202”
示例 2:
输入: num = -7
输出: “-10”
提示:
-107 <= num <= 107
class Solution {
public String convertToBase7(int num) {
StringBuilder res = new StringBuilder();
int sign = 0;
if (num < 0) sign = 1;
num = Math.abs(num);
//0
do {
res.append(num % 7);
num /= 7;
} while (num != 0);
res.reverse();
return sign == 1 ? "-" + res.toString() : res.toString();
}
}