首页 > 代码库 > leetcode 171

leetcode 171

171. 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 

从A到Z依次代表从1到26,按照26进制进位。
代码如下:
 1 class Solution { 2 public: 3     int titleToNumber(string s) { 4         int n = s.length(); 5         int m = n; 6         int result = 0; 7         if(n == 0) 8         { 9             return 0;10         }11         result = (int)s[m-1];12         result -= 64;13         for(int i = 1; i < n+1; i++)14         {15             m--;16             if(m == 0)17             {18                 return result;19             }20             int p = 1;21             for(int j = 0; j < i; j++)22             {23                 p *= 26;24             }25             int re = (int)s[m-1];26             re -= 64;27             result = result + re * p;28         }29         return result;30     }31 };

 

 

leetcode 171