【力扣】L504 - 七进制数

class Solution {
    public String convertToBase7(int n) {
      boolean ok = true ;
        String ans = "";
        
        if (n == 0 ) {
            ans = "0";
            return ans;
        }
        
        if (n < 0) {
            ok = false;
            n = Math.abs(n);
        }
        while(n > 0) {
            int cur = n%7;
            n = n/7;
            ans = cur + ans;
            
        }
        
        if (ok == false) {
            ans = "-" + ans;
        }
        return ans;


    }
}

效率低


image.png

你可能感兴趣的:(【力扣】L504 - 七进制数)