Last active
August 29, 2015 14:18
-
-
Save walkingtospace/b7ccdabf3f5c317da76e to your computer and use it in GitHub Desktop.
Excel sheet column number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://leetcode.com/problems/excel-sheet-column-number/ | |
//Key point: this problem is just 26th-decimal transformation. | |
//input : only capital? no exception? | |
//O(l); l is the length of the given input | |
//test case | |
//A, Z, AA, BB, AAA | |
// trial: one time accepted! | |
class Solution { | |
public: | |
int titleToNumber(string s) { | |
map<char, int> dic; | |
int res = 0; | |
dic.insert(std::pair<char,int>('A',1)); | |
dic.insert(std::pair<char,int>('B',2)); | |
dic.insert(std::pair<char,int>('C',3)); | |
dic.insert(std::pair<char,int>('D',4)); | |
dic.insert(std::pair<char,int>('E',5)); | |
dic.insert(std::pair<char,int>('F',6)); | |
dic.insert(std::pair<char,int>('G',7)); | |
dic.insert(std::pair<char,int>('H',8)); | |
dic.insert(std::pair<char,int>('I',9)); | |
dic.insert(std::pair<char,int>('J',10)); | |
dic.insert(std::pair<char,int>('K',11)); | |
dic.insert(std::pair<char,int>('L',12)); | |
dic.insert(std::pair<char,int>('M',13)); | |
dic.insert(std::pair<char,int>('N',14)); | |
dic.insert(std::pair<char,int>('O',15)); | |
dic.insert(std::pair<char,int>('P',16)); | |
dic.insert(std::pair<char,int>('Q',17)); | |
dic.insert(std::pair<char,int>('R',18)); | |
dic.insert(std::pair<char,int>('S',19)); | |
dic.insert(std::pair<char,int>('T',20)); | |
dic.insert(std::pair<char,int>('U',21)); | |
dic.insert(std::pair<char,int>('V',22)); | |
dic.insert(std::pair<char,int>('W',23)); | |
dic.insert(std::pair<char,int>('X',24)); | |
dic.insert(std::pair<char,int>('Y',25)); | |
dic.insert(std::pair<char,int>('Z',26)); | |
for(int i=0; i<s.length(); ++i) { | |
res += (int)pow(26.0, (s.length()-1-i))*dic[s[i]]; | |
} | |
return res; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment