LeetCode/solutions/168. Excel Sheet Column Title.md
2019-09-13 23:08:41 +08:00

19 lines
608 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [168. Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/description/)
# 思路
题目的要求相当于是十进制转二十六进制。用一个循环每次对n取模然后n除26进入下一次循环即可。
不过需要注意的是题目给的是1-26对应A-Z而不是0-25对应A-Z所以每次循环时都要对n作自减操作。
# C++
``` C++
class Solution {
public:
string convertToTitle(int n) {
string res;
while (n > 0) {
res = (char)('A' + (--n) % 26) + res;
n /= 26;
}
return res;
}
};
```