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;

    }

};

 

你可能感兴趣的:(convert)