How to compute from high digit to low digit?


  • 0
    B

    My wrong answer

     public String convertToTitle(int n) {
        StringBuilder sb = new StringBuilder();
        int k = getPower(n);
        while(n>0){
            int base = (int)Math.pow(26,k);
            int numofbase = numofbase = (n-1)/base;
            sb.append((char)(numofbase+65));
            n -= (numofbase+1)*base;
            k--;
        }
        
        return sb.toString();
    }
    public int getPower(int x){
        int ret = 0;
        x--;
        while(x>26){
            ret++;
            x/= 26;
        }
        return ret;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.