首页 > 代码库 > 【leetcode】Excel Sheet Column Number

【leetcode】Excel Sheet Column Number

题目如下:

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 

可以看出规律是,如果字符串是ABA的话,26*28+1

public class Solution {
    public int titleToNumber(String s) {
        int n=s.length();
          int result=0;
          for(int i=0;i<n;i++)
          {
             int num=s.charAt(i)-'A'+1;             
             result=result*26+num;        
        }
        return result;
    }
}

【leetcode】Excel Sheet Column Number