Problem
Given a column title as appear in an Excel sheet, return its corresponding column number.
Example
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Input: "A"
Output: 1
Input: "AB"
Output: 28
Input: "ZY"
Output: 701
Code
static int var = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
int titleToNumber(string s) {
int res = 0;
for(int i=0;i<s.size();i++){
res = res*26 + (s[i]-64);
}
return res;
}
};
网友评论