Leet Code OJ 171. Excel Sheet Column Number [Difficulty: Easy]

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

翻译:
关联问题:“Excel Sheet Column Title”
给定一个类似Excel里面的列标题,返回它的列数值。

分析:
先将单个字母转换为数字,问题就转化为26进制的数转10进制的数。

代码:

public class Solution {
    public int titleToNumber(String s) {
        char[] arr=s.toCharArray();
        int sum=0;
        for(char c:arr){
            int num=c-'A'+1;
            if(num<=26&&num>0){
                sum=sum*26+num;
            }
        }
        return sum;
    }
}

你可能感兴趣的:(算法,Excel)