Simple Python solution(60 ms) -- easy to understand


  • 0
    R
    class Solution(object):
         def titleToNumber(self, s):
            key = 0
            l = len(s)
            count = 0
            if not s:
                return s
            else:
                for i in s:
                    key += (ord(i)-64)*pow(26, l-1-count)
                    count += 1
                return key

  • 0
    M

    my method is similar with yours, but my solution costs 110ms.. here is my solution. Could you help me figure out the reason?

    class Solution(object):
        def titleToNumber(self, s):
            """
            :type s: str
            :rtype: int
            """
            n = len(s)
            sum = 0
            count = 0
            for i in s:
                count += 1
                sum = sum + (ord(i)-64)*(26**(n-count))
            return sum

  • 0
    C

    Ming, the server load greatly influences the benchmarks, resulting in large variations run to run.

    I've just tried submitting your solution, and I see much better results: 68ms.

    And incidentally, OP solution I'm seeing worse: 100ms.

    Hope that helps.


Log in to reply
 

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