171. Excel Sheet Column Number
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
C++(6ms):
1 class Solution { 2 public: 3 int titleToNumber(string s) { 4 int ans = 0 ; 5 int k = 0 ; 6 for (int i = s.size()-1; i >= 0;i-- ){ 7 ans += (s[i]-'A'+1)*pow(26,k++); 8 } 9 return ans ; 10 } 11 };
C++(3ms):
1 class Solution { 2 public: 3 int titleToNumber(string s) { 4 int ans = 0 ; 5 int k = 0 ; 6 for (int i = 0; i < s.size();i++ ){ 7 ans = ans * 26 + (s[i]-'A'+1); 8 } 9 return ans ; 10 } 11 };