您的位置:首页 > 职场人生

Leetcode: Excel Sheet Column Title

2014-12-21 20:27 253 查看
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


以前面试遇到过,磕磕巴巴写出来http://blog.csdn.net/ymhhym/article/details/23594753;重写,比较清楚简短。

class Solution {
public:
string convertToTitle(int n) {
string result;
while (n > 0) {
int rmod = n % 26;
n /= 26;
if (rmod == 0) {
result.push_back('Z');
n -= 1;
}
else {
result.push_back('A' + rmod - 1);
}

}
reverse(result.begin(), result.end());

return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 面试