mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
19 lines
604 B
Markdown
19 lines
604 B
Markdown
![]() |
# [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++
|
|||
|
```
|
|||
|
class Solution {
|
|||
|
public:
|
|||
|
string convertToTitle(int n) {
|
|||
|
string res;
|
|||
|
while (n > 0) {
|
|||
|
res = (char)('A' + (--n) % 26) + res;
|
|||
|
n /= 26;
|
|||
|
}
|
|||
|
return res;
|
|||
|
}
|
|||
|
};
|
|||
|
```
|