标签:leetcode
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
其实就是个进制转换。水水就过。倒是 Python 的代码让我意识到获取字母的 ASCII 码要用 ord 函数,不能直接强制类型转换。
C++:
class Solution { public: int titleToNumber(string s) { int len = s.size(); int ans = 0; for(int i = 0;i < len;++i) ans = ans*26 + s[i] - 'A' + 1; return ans; } };
class Solution: # @param s, a string # @return an integer def titleToNumber(self, s): n = len(s) ans = 0 for i in range(0,n): ans = ans*26 + ord(s[i]) - ord('A') + 1 return ans
【LeetCode】Excel Sheet Column Number
标签:leetcode
原文地址:http://blog.csdn.net/jcjc918/article/details/43027147