LeetCode--171--Excel表列序号
生活随笔
收集整理的這篇文章主要介紹了
LeetCode--171--Excel表列序号
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
問題描述:
給定一個Excel表格中的列名稱,返回其相應的列序號。
例如,
A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 ...示例 1:
輸入: "A" 輸出: 1示例?2:
輸入: "AB" 輸出: 28示例?3:
輸入: "ZY" 輸出: 701方法1:(轉)
1 class Solution(object): 2 def titleToNumber(self, s): 3 """ 4 :type s: str 5 :rtype: int 6 """ 7 sum_ = 0 8 for i in range(len(s)): 9 tmp = ord(s[i]) - 65 + 1 10 sum_ = tmp + 26 * sum_ 11 return sum_ABC = step1:sum_ = 1 + 26 × 0 ;step2:sum_ = 2 + 26 × 1;step3: sum_ = 3 + 26 × 28?
方法2:(轉)
1 class Solution(object): 2 def get_int(self,strs): 3 return ord(strs) - 64 4 5 def titleToNumber(self, s): 6 """ 7 :type s: str 8 :rtype: int 9 """ 10 result = 0 11 s = s[::-1] 12 for i in range(len(s)): 13 if i==0: 14 result +=self.get_int(s[i]) 15 else: 16 result += 26**i*self.get_int(s[i]) 17 return resultABC ->CBA = 3 + 26^1×2 + 26^2×1
2018-09-15?10:03:08
轉載于:https://www.cnblogs.com/NPC-assange/p/9650069.html
總結
以上是生活随笔為你收集整理的LeetCode--171--Excel表列序号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一章 Linux系统简介
- 下一篇: OpenCV中cvWaitKey()函数