My java 3 ms method,any suggestion?


  • 0
    J
     public static int titleToNumber(String s){
        char[] schar = s.toCharArray();
        int length = s.length();
        double sum = 0;
        double y = 26;
        for(int i =schar.length-1;i>=0;i--){
            double x = length-1-i;
            sum = sum + getVal(schar[i])*Math.pow(y,x);
        }
        return (int)sum;
    }
    
    public static int getVal(char c){
        int val = c - 'A' +1;
        return val;
    }

Log in to reply
 

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