LeetCode#171 Excel Sheet Column Number

Problem Definition:

  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
Solution:

1 def titleToNumber( s):
2     s=s[::-1]
3     num=0
4     for i,c in enumerate(s):
5         num+=(ord(c)-64)*(26**i)
6     return num

Use reduce:

1 def titleToNumber( s):
2        return reduce(lambda x,y: x*26+y, (ord(c)-64 for c in s))

Well... the efficiency is not quite as good as expected, specious huh...

 

你可能感兴趣的:(LeetCode)