-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path168_Excel_Sheet_Column.cpp
40 lines (35 loc) · 1.1 KB
/
168_Excel_Sheet_Column.cpp
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
40
//Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.
// A -> 1 , Z -> 26, AA -> 27
//TC & SC = O(log(columnNumber))
#include <bits/stdc++.h>
using namespace std;
string convertToTitle(int colNum) {
string title;
//we will follow 0 based indexing
while(colNum > 0) {
char rem = 'A' + (colNum -1) % 26; // Adjusted to 0-based indexing
title = rem + title; // prepend least significant char to title
colNum = (colNum -1) / 26;
}
return title;
}
/*
string convertToTitle(int colNum) {
string title;
vector<char> alpha = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z'};
while(colNum > 0) {
char rem = (colNum -1) % 26; // Adjusted to 0-based indexing
title =alpha[rem] + title; // prepend least significant char to title
colNum = (colNum -1) / 26;
}
return title;
}
*/
int main() {
int colNum = 28;
cout << convertToTitle(colNum);
return 0;
}