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:
以前面试遇到过,磕磕巴巴写出来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;
}
};
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 168. Excel Sheet Column Title
- LeetCode题解:excel-sheet-column-title
- 【leetcode】168. Excel Sheet Column Title(Python & C++)
- Excel Sheet Column Title -- leetcode
- leetcode Excel Sheet Column Title
- LeetCode Excel Sheet Column Title
- Leetcode 168. Excel Sheet Column Title (Easy) (cpp)
- Leetcode Excel Sheet Column Title
- leetcode-Excel Sheet Column Title
- Leetcode - Excel Sheet Column Title
- Leetcode 168. Excel Sheet Column Title
- 【LeetCode】168.Excel Sheet Column Title
- LeetCode 168. Excel Sheet Column Title
- leetcode 168. Excel Sheet Column Title 171 Excel Sheet Column Number
- LeetCode 168 Excel Sheet Column Title(Excel的列向表标题)
- Leetcode: Excel Sheet Column Title
- LeetCode 168. Excel Sheet Column Title
- (LeetCode)Excel Sheet Column Title --- Excel页码映射
- Leetcode 168. Excel Sheet Column Title
- LeetCode:《Excel Sheet Column Title》