您的位置:首页 > 编程语言 > Python开发

<LeetCode><Easy> 171 Excel Sheet Column Number

2015-10-16 13:34 567 查看
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


#Python2 76ms

class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
s2n=lambda x:ord(x)-64
total=0
length=len(s)
for i in range(length):
total+=pow(26,length-i-1)*s2n(s[i])
return total

#Python2 72ms

class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
s2n=lambda x:ord(x)-64
length=len(s)
return sum(pow(26,length-i-1)*s2n(s[i]) for i in range(length))

#Python2 68ms

class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
s2n=lambda x:ord(x)-64
return sum(pow(26,s.__len__()-i-1)*s2n(s[i]) for i in range(s.__len__()))#Python 84ms

class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
return sum(pow(26,s.__len__()-i-1)*(ord(s[i])-64) for i in range(s.__len__()))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode python