首页 > 代码库 > LeetCode 171 Excel Sheet Column Number

LeetCode 171 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 

 

思路:

相当于26进制转10进制,要注意A从1开始而不是从0开始的边界条件。

 

解法:

public class Solution{    public int titleToNumber(String s)    {        int sum = 0;        int temp;        for(int i = 0; i < s.length(); i++)        {            temp = s.charAt(i) - ‘A‘ + 1;            sum = sum * 26 + temp;        }        return sum;    }}

 

LeetCode 171 Excel Sheet Column Number