Share my solution in java


  • 0
    J
    public class Solution {
    public int titleToNumber(String s) {
        int result = 0;
        s= s.toUpperCase();
        int length = s.length();
        if(length == 1){
            return letterIndex(s.charAt(0));
        }
        int pow = length-1;
        for(int i =0; i< length-1; i++){
            int num = letterIndex(s.charAt(i));
            result += num*Math.pow(26, pow);
            pow--;
        }  
        
        return result + letterIndex(s.charAt(length-1));
    }
    public int letterIndex(char c){
        return c-64;
    }
    

    }


Log in to reply
 

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