leetcode 171. Excel Sheet Column Number

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 

class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """     
        """
        ans = 0   
        for c in s:
            n = ord(c)-ord('A')+1
            ans = ans*26+n
        return ans
        """        
        #return sum((ord(c)-ord('A')+1)*(26**(len(s)-1-i)) for i,c in enumerate(s))
        to_num = lambda c: ord(c)-ord('A')+1        
        return reduce(lambda x,y: x*26+y, [to_num(c) for c in s])

 

posted @ 2018-03-25 12:15  bonelee  阅读(139)  评论(0编辑  收藏  举报