leetcode171.[math] Excel Sheet Column Number

来源:互联网 发布:高潮是什么体验 知乎 编辑:程序博客网 时间:2024/05/16 04:22

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 -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 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 -> A2 -> B3 -> C26 -> Z27 -> AA28 -> 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
0 0
原创粉丝点击