Skip to content

Commit

Permalink
add Easy_171_Excel_Sheet_Column_Number
Browse files Browse the repository at this point in the history
  • Loading branch information
zongyanqi committed Mar 19, 2017
1 parent 4f488e4 commit 984e54f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Easy_171_Excel_Sheet_Column_Number.js
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'));

0 comments on commit 984e54f

Please sign in to comment.