LeetCode - Excel Sheet Column Title
2015-01-23 17:31
225 查看
Excel Sheet Column Title
2015.1.23 17:20
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
Solution:
The code below is self-explanatory. It is a piecewise function.
Total time is O(log(n)), only in that the logarithm base here is 26.
Accepted code:
2015.1.23 17:20
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
Solution:
The code below is self-explanatory. It is a piecewise function.
Total time is O(log(n)), only in that the logarithm base here is 26.
Accepted code:
// 1CE, 4WA, 1AC, so careless class Solution { public: string convertToTitle(int n) { long long int n1 = n; long long int base = 26; int len = 1; --n1; while (n1 >= base) { n1 -= base; base *= 26; ++len; } string s; base /= 26; while (base > 0) { s.push_back(n1 / base + 'A'); n1 %= base; base /= 26; } return s; } };
相关文章推荐
- LeetCode-Excel Sheet Column Title
- 【leetcode】【168】Excel Sheet Column Title
- 【leetcode 进制转换】Excel Sheet Column Title
- [LeetCode] Excel Sheet Column Title
- LeetCode.158(171) Excel Sheet Column Title&Excel Sheet Column Number
- Leetcode:Excel Sheet Column Title
- LeetCode || Excel Sheet Column Title
- [leetCode]168. Excel Sheet Column Title
- LeetCode 168: Excel Sheet Column Title
- LeetCode: Excel Sheet Column Title
- LeetCode-Excel Sheet Column Title-解题报告
- Leetcode 168. Excel Sheet Column Title
- LeetCode Excel Sheet Column Title
- [leetcode] 168. Excel Sheet Column Title
- LeetCode|Excel Sheet Column Number&& Excel Sheet Column Title
- leetcode 168. Excel Sheet Column Title
- leetcode 168. Excel Sheet Column Title
- leetcode---Excel Sheet Column Title
- leetcode 154: Excel Sheet Column Title
- Excel Sheet Column Title -- leetcode