[LeetCode] 171. Excel Sheet Column Number 求Excel表列序号
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
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
168. Excel Sheet Column Title 的类似题目,这次是给出列的题目,返回对应的数字。
Python:
1 2 3 4 5 6 7 | class Solution( object ): def titleToNumber( self , s): result = 0 for i in xrange ( len (s)): result * = 26 result + = ord (s[i]) - ord ( 'A' ) + 1 return result |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Solution { public : int titleToNumber(string s) { int n = s.size(); int res = 0; int tmp = 1; for ( int i = n; i >= 1; --i) { res += (s[i - 1] - 'A' + 1) * tmp; tmp *= 26; } return res; } }; |
类似题目:
[LeetCode] 168. Excel Sheet Column Title 求Excel表列名称
All LeetCode Questions List 题目汇总
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步