Java Solution - 2 ms [Accepted]


  • 0
    S

    Excel column titles can be considered as a number representing system with base as 26
    where A=1, B=2..., Z=26.

    public int titleToNumber(String s) {
            if(s==null || s.equals("")) return 0;
            
            int counter=0;
            int column=0;
    
            for(int i=s.length()-1;i>-1;i--,counter++){
                // similar to what we do when we convert number from binary to decimal
                column+= Math.pow(26,counter) * ((int)s.charAt(i) - 64);
            }
            return column;
        }
    

Log in to reply
 

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