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):
Array_=list(s)
Array_.reverse()
s=0
for i in range(len(Array_)):
s+=(ord(Array_[i])-ord('A')+1)*(26**i)
return s
leetcode 168.[math] Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A
2 -> B
3 -> C
26 -> Z
27 -> AA
28 -> AB
class Solution(object):
def convertToTitle(self, n):
dict=['Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y']
ans=[]
while n!=0:
ans.append(dict[n%len(dict)])
tmp=n%len(dict)
if tmp==0:
tmp=len(dict)
n=(n-tmp)/len(dict)
ans.reverse()
s=''.join(ans)
return s