Share my python solution use int(i, base=36) and simple JAVA solution.


  • 5
    T
    class Solution:
    # @param s, a string
    # @return an integer
    def titleToNumber(self, s):
        res = 0
        for i in s:
            res = 26*res + int(i, 36) - 9
        return res
    

    One line

    class Solution:
    # @param s, a string
    # @return an integer
    def titleToNumber(self, s):
        return sum(26**n*(int(i, 36)-9) for n, i in enumerate(s[::-1]))
    

    JAVA

        public int titleToNumber(String s) {
        int res = 0;
        for (char c: s.toCharArray())
            res = 26*res + c-'A'+1;
    
        return res;
        }

Log in to reply
 

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