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 cases.

Subscribe to see which companies asked this question

Tags

Math

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

因爲這裏的值是以1爲基準的,而不是0,相當於26進制的數都整體減1,才能對應上從0開始的十進制數。

/**
 * @param {number} n
 * @return {string}
 */
var convertToTitle = function(n) {
    if(n<=26) {
        return String.fromCharCode(64 + n);
    }
    
    var ret = "";
    while(n > 0) {
        n--;
        var a = n % 26;
        ret = String.fromCharCode(65 + a) + ret;
        n = Math.floor(n/26);
    }
    
    return ret;
};




發佈了82 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章