题目链接

思路:

此题与168题解法刚好相反。但都是使用26进制数。使用26进制来求解,类比于二进制转十进制,此题为26进制转十进制。

从字符串最后一个字符开始向前遍历,统计每一个位置的字母所代表的数字的大小,全部加起来,返回即可。

举例:"ABCD",A所在位置代表26^3,B所在位置代表26^2,C所在位置代表26^1,D所在位置代表26^0

           A代表1,B代表2,C代表3,D代表4

          所以最终结果为,1x26^3 + 2x26^2 + 3x26^1 + 4x26^0 = 19010

    public int titleToNumber(String s) {if (s == null || s.equals("")) {return 0;}int result = 0;int base = 1;for (int i = s.length() - 1; i > -1; i--) {result += (s.charAt(i) - 'A' + 1) * base;base *= 26;}return result;}

 

DY_33
原创文章 43获赞 0访问量 397
关注私信
展开阅读全文