168.Excel Sheet Column Title
168.Excel Sheet Column Title
难度:Easy
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
1
For example:
2
3
1 -> A
4
2 -> B
5
3 -> C
6
...
7
26 -> Z
8
27 -> AA
9
28 -> AB
10
...
11
Example 1:
12
13
Input: 1
14
Output: "A"
15
Example 2:
16
17
Input: 28
18
Output: "AB"
19
Example 3:
20
21
Input: 701
22
Output: "ZY"
Copied!
实际上是26进制的数的转换,但是起始点是1开始,所以每次循环取模时,需要-1.
1
class Solution {
2
public:
3
string convertToTitle(int n) {
4
string res;
5
while(n>26)
6
{
7
int t=(n-1)%26;
8
cout<<t<<endl;
9
res= char('A'+t) +res;
10
n=(n-t)/26;
11
}
12
// cout<<n<<endl;
13
return char('A' +n-1) + res;
14
}
15
};
Copied!
执行用时 :4 ms, 在所有 C++ 提交中击败了72.89%的用户 内存消耗 :8 MB, 在所有 C++ 提交中击败了63.84%的用户
Copy link