Excel Sheet Column Number (E)
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
题意
将给定字母字符串转换为对应的整数。
思路
26进制转换。
代码实现
class Solution {
public int titleToNumber(String s) {
int ans = 0;
for (int i = 0; i < s.length(); i++) {
int x = s.charAt(i) - 'A' + 1;
ans = ans * 26 + x;
}
return ans;
}
}
本文介绍了一种算法,用于将Excel工作表中的列标题转换为其对应的列号。例如,'A'转换为1,'AB'转换为28等。通过26进制转换的思路,提供了一个简洁的Java代码实现。
484

被折叠的 条评论
为什么被折叠?



