顿搜
飞过闲红千叶,夕岸在哪
类目归类
Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
public int titleToNumber(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int result = 0;
int times = 1;
for (int i = s.length() - 1; i >= 0; i--) {
result += (s.charAt(i) + 1 - 'A') * times;
times *= 26;
}
return result;
}public class LeetCode0171 {
public int titleToNumber(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int result = 0;
int times = 1;
for (int i = s.length() - 1; i >= 0; i--) {
result += (s.charAt(i) + 1 - 'A') * times;
times *= 26;
}
return result;
}
public static void main(String[] args) {
LeetCode0171 leetcode = new LeetCode0171();
System.out.println(leetcode.titleToNumber("AC"));
}
}