Excel Sheet Column Number

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

 

answer:

class Solution { public:     int titleToNumber(string s) {     int res=0,i,n=s.length(),k=1;     char a;     //return n;     for(i=n-1;i>=0;i--)     {        // return i;         a = s[i];         res = res + (int(a-'A') + 1) * k ;         k *= 26;     }     return res;     } };

posted @ 2015-11-25 16:22  djiankuo  阅读(136)  评论(0编辑  收藏  举报