Excel Sheet Column Number--LeetCode

时间:2015-04-12 10:40:00   收藏:0   阅读:104

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 

Credits:

思路:相当于是26进制之间的转换

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int titleToNumber(string& s) 
{
    int len = s.length();
    int i;
    int result =0;
    int temp;
    for(i=len-1;i>=0;i--)
    {
                         
          if(i == len-1)
            temp =1;
          else
              temp = temp*26;
          result += temp*(s[i]-'A'+1);        
    } 
    return result;   
}

int main()
{
    string str("ABC");
    cout<<titleToNumber(str)<<endl;
    system("pause");
    return 0;
}


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!