Excel Sheet Column Number

来源:互联网 发布:淘宝能在1688 编辑:程序博客网 时间:2024/06/05 05:35

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进制,注意以'A'而不是0开头,因此要“+1”,跟二进制转换成十进制一样。

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


0 0
原创粉丝点击