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
Hide Tags: Math
解题思路:
A—Z 26个字母,AA出现27,则进制为26,。因此只需将给定字符串从高为到低位依次遍历,然后运算即可。
代码如下:
public static int titleToNumber(String s) { int sum=(int)s.charAt(0)-64; for (int i = 1; i < s.length(); i++) { sum=sum*26+(int)(s.charAt(i)-64); } return sum; }
时间: 2024-12-26 19:32:05