给定一个整数 num,将其转化为 7 进制,并以字符串形式输出。

    示例 1:

    输入: num = 100
    输出: “202”
    示例 2:

    输入: num = -7
    输出: “-10”

    提示:

    -107 <= num <= 107


    1. class Solution {
    2. public String convertToBase7(int num) {
    3. StringBuilder res = new StringBuilder();
    4. int sign = 0;
    5. if (num < 0) sign = 1;
    6. num = Math.abs(num);
    7. //0
    8. do {
    9. res.append(num % 7);
    10. num /= 7;
    11. } while (num != 0);
    12. res.reverse();
    13. return sign == 1 ? "-" + res.toString() : res.toString();
    14. }
    15. }