171. Excel Sheet Column Number
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
...
Example 1:
Input: "A"
Output: 1
Example 2:
Input: "AB"
Output: 28
Example 3:
Input: "ZY"
Output: 701
解题要点:
把string拆分为char数组,从后往前遍历整个数组,每一次都乘以26,依次类推。
class Solution {
public int titleToNumber(String s) {
int res = 0;
int out = 1;
char[] c = s.toCharArray();
for(int i = c.length - 1; i >= 0; i--){
int temp = c[i] - 'A' + 1;
res = res + temp * out;
out *= 26;
}
return res;
}
}
Last updated
Was this helpful?