题目:
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题解:
不解释。
C++版:
class Solution {
public:
int titleToNumber(string s) {
int r = 0;
for(int i = 0; i < s.length(); i++) {
r = r * 26 + (s[i] - 'A') + 1;
}
return r;
}
};
Java版:
public class Solution {
public int titleToNumber(String s) {
int r = 0;
for(int i = 0; i < s.length(); i++) {
r = r * 26 + s.charAt(i) - 'A' + 1;
}
return r;
}
}
Python版:
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
r = 0
for i in s:
r = r * 26 + ord(i) - ord("A") + 1
return r