Simple Java Solution with comments


  • 1
    public String convertToTitle(int n) {
            StringBuilder s = new StringBuilder();
            int len=1;
            //Find the length of final String
            double temp = n;
            while(temp>26){
                len++;
                temp=(temp-26)/26;
            }
            //Find each character of String
            while(len>0){
                int num = n%26;
                if(num==0)num=26;//case when num is 0
                char ch = (char) ('A'+ num-1);
                s.append(ch);
                len--;
                n=(n-num)/26;
            }
            //reverse the string as we are finding from right to left
            return s.reverse().toString();
        }
    

Log in to reply
 

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