-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathExcelSheetColumnTitle.js
48 lines (44 loc) · 1.36 KB
/
ExcelSheetColumnTitle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Source : https://oj.leetcode.com/problems/excel-sheet-column-title/
// Author : Dean Shi
// Date : 2015-06-18
/**********************************************************************************
*
* 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.
*
**********************************************************************************/
/**
* @param {number} n
* @return {string}
*/
var convertToTitle = function(n) {
var result = '';
var code = 65; //'A'.charCodeAt()
// loop when n is greater than 0
while (n) {
// (n - 1) % 26 will return a num on range 0 ~ 25
// 0 + A.charCodeAt() = A.charCodeAt()
// 25 + A.charCodeAt() = Z.charCodeAt()
result = String.fromCharCode((n - 1) % 26 + code) + result;
n = ~~((n - 1) / 26);
}
return result;
};
// Test cases
console.log(convertToTitle(1) === 'A'); // true
console.log(convertToTitle(2) === 'B'); // true
console.log(convertToTitle(3) === 'C'); // true
console.log(convertToTitle(26) === 'Z'); // true
console.log(convertToTitle(27) === 'AA'); // true
console.log(convertToTitle(28) === 'AB'); // true