-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
70 lines (49 loc) · 916 Bytes
/
script.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Problem 29
Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Example 1:
Input: columnNumber = 1
Output: "A"
Example 2:
Input: columnNumber = 28
Output: "AB"
Example 3:
Input: columnNumber = 701
Output: "ZY"
*/
// Solution 1
function numberToChars(num){
// base case
if(num < 27) return String.fromCharCode(num + 64);
let s = '';
let temp = num % 26;
while(num > 0){
temp = temp === 0 ? 26 : temp;
s = String.fromCharCode(temp + 64) + s;
num -= 26;
num /= 26;
}
return s;
}
console.log(numberToChars(703))
// Solution 2
var convertToTitle = function (n) {
var A = 'A'.charCodeAt(0);
var str = '';
while (n > 0) {
n--;
str = String.fromCharCode(A + (n % 26)) + str;
n = parseInt(n / 26);
}
console.log(str);
};
convertToTitle(10);