[LeetCode][JavaScript]Excel Sheet Column Title

https://leetcode.com/problems/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


我恨进制转换,傻傻算不清。
/**
 * @param {number} n
 * @return {string}
 */
var convertToTitle = function(n) {
    var res = "";
    var codeA = "A".charCodeAt();
    while(n > 0){
        n--;
        res = String.fromCharCode(codeA + n % 26) + res;
        n = parseInt(n / 26);
    }
    return res;
};
				
时间: 2024-10-12 06:33:44

[LeetCode][JavaScript]Excel Sheet Column Title的相关文章

[LeetCode][JavaScript]Excel Sheet Column Number

Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 进制转换,比兄弟题简单. 1 /** 2 * @param {string}

[LeetCode]62. Excel Sheet Column Title Excel列序号

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 Credits:Special thanks to @ifanchu for adding this problem and creating all test

Leetcode 168 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 Credits:Special thanks to @ifanchu for adding this problem and creating all test

【LeetCode】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 Solution:经过分析不难发现实际上就是数对于 26 进制的转换用字母表示出来.代码如下: 1 class Solution: 2 # @param {in

leetCode(66)-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 思路: 题意:求对应的数字和字母换算之间的关系 1对应的该是第二个字母B,然而对应A,进位之后应该是BA,实际是AA,由此判断字母也是26进制,只是始终

Java for LeetCode 168 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 解题思路: JAVA实现如下: static public String convertToTitle(int n) { S

leetcode 154: 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 Credits: Special thanks to @ifanchu for adding this problem and creating all tes

【leetcode】Excel Sheet Column Title & Excel Sheet Column Number (easy)

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 思路: 相当于10进制转26进制.与一般不一样的是10进制对应的是0 - 9.而这个26进制对应的是 A(1)- Z(26), 没有0. 我的代码: strin

Leetcode 168 Excel Sheet Column Title 进制数转化

题意:将数字转化成excel表中的行中的项目 本质是10进制转化为26进制,但是在中间加入了一个不一样的操作,在每次操作前都需要n-- 1 class Solution { 2 public: 3 string convertToTitle(int n) { 4 string s(""); 5 for(; n--; n/=26){ 6 s += string("A"); 7 s[s.size() - 1] += n% 26; 8 } 9 reverse(s.begi