Skip to content

Commit 984e54f

Browse files
author
zongyanqi
committed
add Easy_171_Excel_Sheet_Column_Number
1 parent 4f488e4 commit 984e54f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Easy_171_Excel_Sheet_Column_Number.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Given a column title as appear in an Excel sheet, return its corresponding column number.
3+
4+
For example:
5+
6+
A -> 1
7+
B -> 2
8+
C -> 3
9+
...
10+
Z -> 26
11+
AA -> 27
12+
AB -> 28
13+
14+
*/
15+
16+
/**
17+
* @param {string} s
18+
* @return {number}
19+
*/
20+
var titleToNumber = function (s) {
21+
22+
var num = 0;
23+
var aCode = 'A'.charCodeAt(0);
24+
for (var i = 0; i < s.length; i++) {
25+
var n = 1 + s.charCodeAt(i) - aCode;
26+
num = num * 26 + n;
27+
}
28+
return num;
29+
};
30+
31+
console.log(titleToNumber('AA'));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy