程序员人生 网站导航

Leetcode 171 Excel Sheet Column Number

栏目:php教程时间:2017-02-21 08:44:36

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 
字符串转数字

类似于进制转换,可以理解为26进制转10进制

class Solution {
public:
    int titleToNumber(string s) {
        int res = 0 , base = 1;
        for(int i = s.size()⑴; i >= 0; i--)
        {
            res += base*(s[i]+1-'A');
            base*=26;
        }
        return res;
    }
};



------分隔线----------------------------
------分隔线----------------------------

最新技术推荐