给你一个字符串 columnTitle ,表示 Excel 表格中的列名称。返回该列名称对应的列序号。

 

例如,

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 
    ...

 

示例 1:

输入: columnTitle = "A"
输出: 1

示例 2:

输入: columnTitle = "AB"
输出: 28

示例 3:

输入: columnTitle = "ZY"
输出: 701

示例 4:

输入: columnTitle = "FXSHRXW"
输出: 2147483647

 

提示:

  • 1 <= columnTitle.length <= 7
  • columnTitle 仅由大写英文组成
  • columnTitle 在范围 ["A", "FXSHRXW"]
Related Topics
  • 数学
  • 字符串
  • \n
  • 👍 259
  • 👎 0
  • 题解

    
    
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int titleToNumber(String columnTitle) {
            int num = 0;
            char[] chars = columnTitle.toCharArray();
            int pow = 1;
            for (int index = chars.length-1; index >= 0; index--) {
                num += (chars[index]-64)*pow;
                pow *= 26;
            }
            return num;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)
    

    已知字母一共26位