My Java Solution 4ms (4 lines)


  • 4
    A

    public class Solution {

    public int titleToNumber(String s) {
        int sum = 0;
        for(int i = 0; i < s.length(); i++ )
            sum = sum + (int)Math.pow(26,s.length() - i - 1) * (s.charAt(i) - 'A' + 1);
        return sum;
    }
    

    }


  • 2
    C

    This is the most excellent solution I have ever seen. You must come from cmu's ebiz program!!


  • 0
    L
    This post is deleted!

  • 0
    C

    excellent! how can you come up with such perfect solution?


  • 0
    A

    because CMU ebiz program is Aweeesommme!


  • 0
    C

    It actually has a problem, if you test lower case letters, i.e. "ab", it returns wrong result.

    Apparently, the test input file does not contain lower case letters, so the solution passed.


  • 0
    A

    yea.
    but this question only ask to " Given a column title as appear in an Excel sheet, "
    because excel design that all the column title are in uppercase, so I didn't change

    to solve the problem you mentioned, only adding a line to change all letters in a string to upper class should be ok.


  • 0
    Y

    I use the same method but your code is more neat! By the way, I think checking null firstly will be better.


Log in to reply
 

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