-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path168.cpp
More file actions
33 lines (26 loc) · 723 Bytes
/
168.cpp
File metadata and controls
33 lines (26 loc) · 723 Bytes
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
// 168. Excel Sheet Column Title - https://leetcode.com/problems/excel-sheet-column-title
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string convertToTitle(int n) {
string answer = "";
while(n > 0) {
--n;
char ch = (n % 26) + 'A';
answer += ch;
n /= 26;
}
reverse(answer.begin(), answer.end());
return answer;
}
};
int main() {
ios::sync_with_stdio(false);
Solution solution;
assert(solution.convertToTitle(1) == "A");
assert(solution.convertToTitle(26) == "Z");
assert(solution.convertToTitle(27) == "AA");
assert(solution.convertToTitle(28) == "AB");
return 0;
}