Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
</pre>
解題思路
這道題其實很像一道進(jìn)制轉(zhuǎn)換。
個位上的A代表1稿黄,B代表2聪蘸,...,Z代表26
十位上的A代表26*1疾党,B代表26*2,...适秩,Z代表26*26
以此類推...
于是代碼就呼之欲出了~
代碼
class Solution {
public:
int titleToNumber(string s) {
int res = 0;
int len = s.length();
for (int i=0;i<len;i++){
res = res*26+s[i]-'A'+1;
}
return res;
}
};