题目地址
https://leetcode.com/problems/excel-sheet-column-number/description/
题目描述
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
Constraints:
1 <= s.length <= 7
s consists only of uppercase English letters.
s is between "A" and "FXSHRXW".
思路
本质是26进制到10进制的转换.
关键点
注意, A对应的是1, 因此每一位sc[i] - 'A' + 1.
代码
- 语言支持:Java
class Solution {
public int titleToNumber(String s) {
char[] sc = s.toCharArray();
int res = 0;
for (int i = 0; i < sc.length; i++) {
res = res * 26 + sc[i] - 'A' + 1;
}
return res;
}
}
网友评论