mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
Create 168. Excel Sheet Column Title.md
This commit is contained in:
parent
f1ae30a3f0
commit
74c2c5ba26
18
168. Excel Sheet Column Title.md
Normal file
18
168. Excel Sheet Column Title.md
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# [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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user