-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcelSheetColumnNumber.js
More file actions
43 lines (40 loc) · 1.01 KB
/
excelSheetColumnNumber.js
File metadata and controls
43 lines (40 loc) · 1.01 KB
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
////////////////////////////////////////////////Excel Sheet Column Number/////////////////////////////////////////////
// Given a string columnTitle that represents the 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
// ...
// Example 1:
//
// Input: columnTitle = "A"
// Output: 1
// Example 2:
// Input: columnTitle = "AB"
// Output: 28
// Example 3:
//
// Input: columnTitle = "ZY"
// Output: 701
// Example 4:
// Input: columnTitle = "FXSHRXW"
// Output: 2147483647
/**
* @param {string} columnTitle
* @return {number}
*/
const titleToNumber = function(columnTitle) {
let res = 0, index = 0;
for(let i = columnTitle.length - 1; i >= 0; i--){
res += Math.pow(26, index++) * (columnTitle[i].charCodeAt(0) - 65 + 1)
}
return res;
};
// console.log(titleToNumber("A"));
// console.log(titleToNumber("AB"));
// console.log(titleToNumber("ZY"));
// console.log(titleToNumber("FXSHRXW"));