题目链接:excel-sheet-column-number
/**
* Related to question Excel Sheet Column Title
* 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
*
*
*/
public class ExcelSheetColumnNumber {
// 1000 / 1000 test cases passed.
// Status: Accepted
// Runtime: 208 ms
// Submitted: 0 minutes ago
static int titleToNumber(String s) {
int colNumber = 0;
int radix = 1;
for (int i = 0; i < s.length(); i++) {
colNumber += (s.charAt(s.length() - i - 1) - 'A' + 1) * radix;
radix *= 26;
}
return colNumber;
}
public static void main(String[] args) {
System.out.println(titleToNumber("A"));
}
}