Leetcode:171. Excel Sheet Column Number 求Excel表字母对应的行号

来源:互联网 发布:js input自动获取焦点 编辑:程序博客网 时间:2024/06/06 20:58

描述:
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 -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 

代码和思路:

/**     *如果为AB则返回1*26+2     *如果是BA则返回2*26+1     *如果是CA则返回3*26+1     *由此得出以下的代码      */    public static int titleToNumber(String s) {        int result = 0;        for (int i = 0; i < s.length(); i++)             result = result * 26 + (s.charAt(i) - 'A' + 1);        return result;    }