Python Function to Convert Excel Sheet Column Titles to Numbers
- 时间:2020-09-07 12:26:38
- 分类:网络文摘
- 阅读:92 次

microsoft-excel-03
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…
Example 1:
Input: “A”
Output: 1Example 2:
Input: “AB”
Output: 28Example 3:
Input: “ZY”
Output: 701Constraints:
1 <= s.length <= 7
s consists only of uppercase English letters.
s is between “A” and “FXSHRXW”.
Compute the Excel Sheet Column Number using Python Iterative Function
The following is a simple Python function that takes a column title, and compute the base-26 numeric values. As the number starts from 1, we have to shift the value by one.
1 2 3 4 5 6 | class Solution: def titleToNumber(self, s: str) -> int: ans = 0 for i in s: ans = ans * 26 + ord(i) - 64 return ans |
class Solution: def titleToNumber(self, s: str) -> int: ans = 0 for i in s: ans = ans * 26 + ord(i) - 64 return ans
The runtime complexity is O(N) where N is the length of the string – given the length is 7 we can also say the complexity is O(1). The space requirement is O(1) constant.
The C++ implementation and to convert backwards from the column numbers to excel titles: Excel Sheet Column Number and Title Conversion in C++
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:wordpress 文章发布出现“定时发布失败”的原因及解决方法 wordpress 数据库表前缀修改方法 wordpress技巧:为标签链接添加rel=”nofollow”属性 wordpress 4.0 新增自定义图标插件安装 WordPress在线文件管理插件:FileBrowser 设置wordpress文章标题高亮的代码 免插件实现WordPress文章置顶的方法 让新浪微博变身外链图床的wordpress插件:微博图床 三款自动翻译文章标题为英文的wordpress插件介绍与比较 WodrPress实现远程图片本地化插件-Auto_Save_Image
- 评论列表
-
- 添加评论