Excel Sheet Column Number (Easy)

Description

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet, return its corresponding column number.

1
2
3
4
5
6
7
8
9
For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28

Analysis

简单题

My Solution

1
2
3
4
5
6
7
8
9
10
11
//C++
class Solution {
public:
int titleToNumber(string s) {
int len = s.length(),ans = 0;
for(int i = 0;i<len;i++){
ans = ans*26+s[i]-'A'+1;
}
return ans;
}
};