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
class Solution {
public:
string convertToTitle(int n) {
string ans = "";
if(n < 1) return "";
while(n){
n --;
char c = n % 26 + 'A';
ans = c + ans;
n /= 26;
}
return ans;
}
};
本文介绍了一种将正整数转换为Excel列标题(如1->A,2->B,...,26->Z,27->AA等)的C++算法实现。通过循环操作和取模运算,将数字转换为对应的字母序列。
1496

被折叠的 条评论
为什么被折叠?



