LeetCodeNo.504七进制

class Solution {
    public String convertToBase7(int num) {
        if(num==0) return "0";
        StringBuilder sb=new StringBuilder();
        int absNum=Math.abs(num);
        while(absNum>0){
            sb.insert(0,absNum%7);//没想到这个方法,打算用reverse()的
            absNum/=7;
        }
        return num>0?sb.toString():"-"+sb.toString();
    }
}

你可能感兴趣的:(LeetCode)