# 168. Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

``````    1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
``````

Example 1:

``````Input: 1
Output: "A"
``````

Example 2:

``````Input: 28
Output: "AB"
``````

Example 3:

``````Input: 701
Output: "ZY"
``````

Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases.

``````class Solution {
public:
string convertToTitle(int n) {
string res = "";
while (n) {
if (n % 26 == 0) {
res += 'Z';
n -= 26;
} else {
res += n % 26 - 1 + 'A';
n -= n % 26;
}
n /= 26;
}
reverse(res.begin(), res.end());
return res;
}
};
``````

``````class Solution {
public:
string convertToTitle(int n) {
string res;
while (n) {
res += --n % 26 + 'A';
n /= 26;
}
return string(res.rbegin(), res.rend());
}
};
``````

``````class Solution {
public:
string convertToTitle(int n) {
return n == 0 ? "" : convertToTitle(n / 26) + (char)(--n % 26 + 'A');
}
};
``````

Excel Sheet Column Number

https://leetcode.com/problems/excel-sheet-column-title/

https://leetcode.com/problems/excel-sheet-column-title/discuss/51399/Accepted-Java-solution

https://leetcode.com/problems/excel-sheet-column-title/discuss/51398/My-1-lines-code-in-Java-C%2B%2B-and-Python

https://leetcode.com/problems/excel-sheet-column-title/discuss/51421/Share-my-simple-solution-just-a-little-trick-to-handle-corner-case-26

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation