Skip to content

Commit fbd6abd

Browse files
authored
VVI
1 parent 9ca7c70 commit fbd6abd

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
string convertToTitle(int columnNumber) {
4+
string res;
5+
while(columnNumber){
6+
columnNumber--;
7+
res += (char)(columnNumber%26 + 'A');
8+
columnNumber/=26;
9+
}
10+
reverse(res.begin(), res.end());
11+
return res;
12+
}
13+
};
14+
15+
/*
16+
Time complexity: O(log⁡N)
17+
The number of operations would be equal to the number of while loops iterations.
18+
In each iteration, the number N gets divided by 26. Hence the time complexity would be O(log⁡26N).
19+
Note that the base of the logarithm is not relevant when it comes to big O, since all logarithms are related by a constant factor.
20+
21+
Space complexity: O(1)
22+
We only need one string to store the output, but generally, the space to store the output is not considered part of space complexity
23+
and hence the space complexity is constant.
24+
*/

0 commit comments

Comments
 (0)