码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode#171 Excel Sheet Column Number

时间:2015-07-19 21:20:40      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:

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#171 Excel Sheet Column Number

标签:

原文地址:http://www.cnblogs.com/acetseng/p/4659444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!