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; }
Excel Sheet Column Number--LeetCode
原文地址:http://blog.csdn.net/yusiguyuan/article/details/45008021