Excel Sheet Column Title Total Accepted: 18284 Total Submissions: 103291 My Submissions Question Solution
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 t;
while(n>0)
{
t+="ZABCDEFGHIJKLMNOPQRSTUVWXY"[n%26];
n-=1;//相当于借了一位
n/=26;
}
string result(t.rbegin(),t.rend());
return result;
}
};
18 / 18 test cases passed.
Status: Accepted
Runtime: 1 ms
速度也是杠杠的~~
本文介绍了一个将正整数转换为Excel工作表中对应的列标题的方法,并提供了一个通过C++实现的具体示例。例如,1对应A,26对应Z,27对应AA等。
976

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



