Simple Java solution with StringBuilder


  • 4
    V
    public String convertToTitle(int n) {
            StringBuilder str = new StringBuilder();
            int remainder = 0;
            while(n > 0){
                remainder = (n-1) % 26;
                str.insert(0, (char)(remainder + 'A'));
                n = (n - remainder) / 26;
            }
            return str.toString();
        }

Log in to reply
 

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