Skip to content

Commit 4fb4d3c

Browse files
authored
Create 168-excel-sheet-column-title.js
1 parent 754440b commit 4fb4d3c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

168-excel-sheet-column-title.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number} n
3+
* @return {string}
4+
*/
5+
const convertToTitle = function(n) {
6+
if (n === 0) {
7+
return ''
8+
}
9+
const res = [];
10+
const hash = {};
11+
('ABCDEFGHIJKLMNOPQRSTUVWXYZ').split('').forEach((el,idx) => {
12+
hash[idx] = el
13+
})
14+
15+
while(n > 0) {
16+
n--;
17+
res.unshift(hash[n % 26]);
18+
n = Math.floor(n / 26);
19+
}
20+
21+
return res.join('')
22+
};

0 commit comments

Comments
 (0)