Create 168. Excel Sheet Column Title.md

This commit is contained in:
唐树森 2018-09-28 23:24:27 +08:00 committed by GitHub
parent f1ae30a3f0
commit 74c2c5ba26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View 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;
}
};
```