标签:
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
思路:
假设num是一个五位数ABCDE.
num=A*26^4+B*26^3+c*26^2+D*26+E*1;
radix作为基数,初始化为1,每次循环*26;
1 class Solution { 2 public: 3 int titleToNumber(string s) { 4 int len=s.length(); 5 if(len==0){ 6 return 0; 7 } 8 int num=0; 9 int radix=1; 10 for(int i=len-1;i>=0;i--){ 11 num+=radix*(s[i]-‘A‘+1); 12 radix*=26; 13 } 14 return num; 15 } 16 };
leetcode:171 Excel Sheet Column Number-每日编程第十题
标签:
原文地址:http://www.cnblogs.com/yishuimao/p/5033308.html