leetcode [#171]

目录

题目

Given a column title as appear in an Excel sheet, return its corresponding column number.

Note:
A -> 1
B -> 2
C -> 3

Z -> 26
AA -> 27
AB -> 28


解决方案

1
2
3
4
5
6
7
public class Solution {
public int titleToNumber(String s) {
double result = 0;
for(int i = 0; i < s.length(); i++) result += Math.pow(26, (s.length() - 1 - i)) * (s.charAt(i) - 'A' + 1);
return (int) result;
}
}

注意事项

  1. 遍历字符串,每一位的权重是26的(s.length() - 1 - i)次幂,结果求和即可。