forked from xiaoyu2er/leetcode-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Easy_171_Excel_Sheet_Column_Number
- Loading branch information
zongyanqi
committed
Mar 19, 2017
1 parent
4f488e4
commit 984e54f
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Given a 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 | ||
*/ | ||
|
||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var titleToNumber = function (s) { | ||
|
||
var num = 0; | ||
var aCode = 'A'.charCodeAt(0); | ||
for (var i = 0; i < s.length; i++) { | ||
var n = 1 + s.charCodeAt(i) - aCode; | ||
num = num * 26 + n; | ||
} | ||
return num; | ||
}; | ||
|
||
console.log(titleToNumber('AA')); |