Excel Sheet Column Number(STRING-TYPE CONVERTION)

QUESTION

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

FIRST TRY

string与数字的转换要借助char

class Solution {
public:
    int titleToNumber(string s) {
        int ret = 0;
        char ch;
        for(int i = 0; i < s.length(); i++)
        {
            ch = s[i]; //string to char
            ret = ret*26 + ch - 'A' + 1;
        }
        return ret;
    }
};

 

posted on 2015-01-03 17:06  joannae  阅读(156)  评论(0编辑  收藏  举报

导航