Excel Sheet Column Number

Question

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28

Tags

  • Mathematics

Thought

Similar to the previous problem and the implementation is below.

Time: O(n) Space: O(1)

Code

class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        ans = 0
        for ch in s:
            ans = ans * 26 + ord(ch) - ord('A') + 1
        return ans

results matching ""

    No results matching ""