Skip to content

Latest commit

 

History

History
55 lines (38 loc) · 1.18 KB

504-base-7.md

File metadata and controls

55 lines (38 loc) · 1.18 KB

504. Base 7 - 七进制数

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

示例 1:

输入: 100
输出: "202"

示例 2:

输入: -7
输出: "-10"

注意: 输入范围是 [-1e7, 1e7] 。


题目标签:

题目链接:LeetCode / LeetCode中国

题解

Language Runtime Memory
java 1 ms 35.7 MB
class Solution {
    public String convertToBase7(int num) {
        if (num == 0) return "0";
        boolean flag = num < 0;
        if (flag) num *= -1;
        StringBuilder sb = new StringBuilder();
        String s = "0123456";
        while (num > 0) {
            sb.append(s.charAt(num % 7));
            num /= 7;
        }
        if (flag) sb.append("-");
        return sb.reverse().toString();
    }
}