Monday, March 28, 2016

LeetCode Q171: Excel Sheet Coumn Numer

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 
class Solution {
public:
int titleToNumber(string s) {
int base=0;
int res=0;
for(int i=s.length()-1; i>=0; i--){
int t=s[i]-'A'+1;
res = res+t*pow(26, base++);
}
return res;
}
};

No comments:

Post a Comment