Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB
Credits:
进制转换,注意当 n%26 == 0的情况。
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
class Solution { | |
public: | |
string convertToTitle(int n) { | |
string str; | |
while(n){ | |
int r=n%26; | |
n=n/26; | |
if(r==0){ //为26的整数倍,该位设置为Z,n减掉1 | |
str+='Z'; | |
n--; | |
}else{ | |
str+=('A'+r-1); | |
} | |
} | |
reverse(str.begin(), str.end()); | |
return str; | |
} | |
}; |
No comments:
Post a Comment