Excel Sheet Column Title
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
解题思路:
十进制转化成26进制。注意对于string类型来说,并不存在string=char+string类型操作符,只有string=string+char操作符。因此,可以用栈来存储低位,然后出栈。
另外一个需要注意的是(n-1)%26+‘A‘,要n-1哈。
class Solution {
public:
string convertToTitle(int n) {
string result="";
stack<char> s;
while(n!=0){
s.push((n-1) % 26 + 'A');
n = (n-1) / 26;
}
while(!s.empty()){
result += s.top();
s.pop();
}
return result;
}
};[LeetCode] Excel Sheet Column Title
原文地址:http://blog.csdn.net/kangrydotnet/article/details/45000509