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
26进制,简单题。
稍微有点难的地方就是这个26进制是从1开始的,而我们通常的机制是从0开始的,仔细思考一下就懂了。
public static int titleToNumber(String s) {
int result = 0;
for (int i = 0; i < s.length(); i++) {
result += (s.charAt(i) - 64) * Math.pow(26, s.length() - i - 1);
}
return result;
}
本文介绍了一个简单的算法,用于将Excel表格中的列标题转换为其对应的26进制列编号。例如,'A'对应1,'Z'对应26,'AA'对应27等。此算法适用于需要进行Excel列号转换的应用场景。
1915

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



