168. Excel Sheet Column Title

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Input: columnNumber = 1
Output: "A"
Input: columnNumber = 28
Output: "AB"
Input: columnNumber = 701
Output: "ZY"
Input: columnNumber = 2147483647
Output: "FXSHRXW"
class Solution {
public:
string convertToTitle(int columnNumber) {
string s;
int val = 0;

while(columnNumber > 0){
val = columnNumber%26;
if(val == 0){
val = 26;
}
s += (val + 64);
columnNumber -= val;
columnNumber /= 26;
}
reverse(s.begin(), s.end());
return s;
}
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store