blob: 3de395f10b7c69b9398abf60b6b4a37701e6e7af (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
// 168. Excel Sheet Column Title
#include "leetcode.h"
/*
* given an integer 'column_number', return its corresponding column title as it
* appears in an excel sheet. for example: A -> 1 B -> 2 C -> 3
* ...
* Z -> 26
* AA -> 27
* AB -> 28
* ...
*/
class Solution {
public:
string convertToTitle(int n) {
if (n < 27)
return string(1, 'A' + (n - 1) % 26);
string ans = "";
while (n > 0) {
if (!(n % 26)) {
ans += 'A' + 25;
n--;
} else {
ans += 'A' + n % 26 - 1;
}
n /= 26;
}
reverse(ans.begin(), ans.end());
return ans;
}
};
int main() {
Solution obj;
cout << obj.convertToTitle(1) << endl; // expect: A
cout << obj.convertToTitle(28) << endl; // expect: AB
cout << obj.convertToTitle(701) << endl; // expect: ZY
}
|