Excel Sheet Column Number:有字母组成的26进制转换成10进制
程序员文章站
2024-03-17 20:47:52
...
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思路:26进制转换成10进制的按位运算,很简单,逐位累加求和即可。
class Solution {
public int titleToNumber(String s) {
if(s.length()==0) return 0;
int sum = 0;
int bit = 1;
for(int i = s.length()-1;i>=0;i--){
sum += ((int)(s.charAt(i)-'A')+1)*bit;
bit *=26;
}
return sum;
}
}
上一篇: [Vue]---vue选项卡案例
下一篇: 看完这篇垃圾回收,和面试官扯皮没问题了