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”
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Excel Sheet Column Title.
class Solution {
public:
string convertToTitle(int n) {
string result = "";
while(n) {
result = (char) (--n % 26 + 'A') + result;
n = n /26;
}
return result;
}
};
典型的进制转换问题,需要注意的是这里是 27 进制,因为最小的“数字”是 Z(26),然后通过 ASCII 码的相关运算还原为字母