Problem:
Solution:
class Solution {
public:
int titleToNumber(string s) {
int size = s.size();
int sol = 0, factor = 1;
for (int i = size -1; i >= 0; i--) {
sol += (s[i] - 'A' + 1)*factor;
factor *= 26;
}
return sol;
}
};
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
Solution:
class Solution {
public:
int titleToNumber(string s) {
int size = s.size();
int sol = 0, factor = 1;
for (int i = size -1; i >= 0; i--) {
sol += (s[i] - 'A' + 1)*factor;
factor *= 26;
}
return sol;
}
};
No comments:
Post a Comment