Leetcode 第168题 Excel Sheet Column Title
2015-11-27 14:59
337 查看
题目:Excel Sheet Column Title
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
题目分析:
题目含义是将进行26进制和十进制的转化,涉及到ASCII码与字符之间的转换思路:
将1-26转换为对应的A到Z之间的字母,再重复进行。注意25,26,27的转化。代码:
*C++:class Solution { public: string convertToTitle(int n) { string s = ""; while (n > 0) { s = (char)(65 + (n - 1) % 26) + s; n = (n - 1) / 26; } return s; } };
Javascript:
/** * @param {number} n * @return {string} */ var convertToTitle = function(n) { var s=""; while(n>0) { var res=(n-1)%26; s=ASCIIToLetter(res)+s;//将数字转化为对应的字符 n=parseInt((n-1)/26);//主要js中的弱类型 需要对结果进行取整 } return s; }; function ASCIIToLetter(num) { return String.fromCharCode(num+65);//将num输出对应的字符 }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解