Excel Sheet Column Number
Total Accepted: 54683 Total Submissions: 140674 Difficulty: Easy
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 };
时间: 2024-10-12 08:16:10