LeetCode 168 Excel Sheet Column Title
2017-05-04 12:37
381 查看
168. Excel Sheet Column Title
Given a positive integer, return its corresponding column title as
appear in an Excel sheet.
For example:
难度EASY。相当于是26进制转换,稍微做点处理。
还有一种更简单的写法,避免用Reverse,直接利用递归栈的思想。
如下:
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
难度EASY。相当于是26进制转换,稍微做点处理。
class Solution { public: string convertToTitle(int n) { string s = ""; while(n) { int x = --n % 26; s += 'A' + x; n /= 26; } reverse(s.begin(),s.end()); return s; } };
还有一种更简单的写法,避免用Reverse,直接利用递归栈的思想。
如下:
class Solution { public: string convertToTitle(int n) { return n == 0 ? "" : convertToTitle(n / 26) + (char) (--n % 26 + 'A'); } };
相关文章推荐
- Java [Leetcode 168]Excel Sheet Column Title
- Excel Sheet Column Title - LeetCode 168
- leetcode 168. Excel Sheet Column Title
- leetcode[168]Excel Sheet Column Title
- leetcode[168]:Excel Sheet Column Title
- leetcode 168 Excel Sheet Column Title C++
- 168. Excel Sheet Column Title Leetcode Python
- [leetcode 168] Excel Sheet Column Title
- leetcode 168. Excel Sheet Column Title | 进制转化
- leetcode-168-Excel Sheet Column Title
- leetcode 168. Excel Sheet Column Title 递归输出,注意倍数是26,不是27
- 【leetcode】168. Excel Sheet Column Title(Python & C++)
- [LeetCode]--168. Excel Sheet Column Title
- leetcode-java-168. Excel Sheet Column Title
- leetcode(168) Excel Sheet Column Title
- [leetcode] 168. Excel Sheet Column Title 解题报告
- leetcode 168 Excel Sheet Column Title
- leetcode 168. Excel Sheet Column Title
- leetcode 168. Excel Sheet Column Title
- 168.[LeetCode] Excel Sheet Column Title