Leetcode 171.Excel Sheet Column
2017-11-12 本文已影响5人
ShutLove
Related to question
Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
思路:26进制转为10进制的表示,注意判断字符串中的每个字符是否都合法。
public int titleToNumber(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int res = 0;
for (int i = 0; i < s.length(); i++) {
int num = s.charAt(i) - 'A' + 1;
if (num < 1 || num > 26) {
return 0;
}
res = res * 26 + num;
}
return res;
}