[LeetCode][JavaScript]Excel Sheet Column Number

Excel Sheet Column Number

Related to question Excel Sheet Column Title

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 

https://leetcode.com/problems/excel-sheet-column-number/

 





进制转换,比兄弟题简单。

 1 /**
 2  * @param {string} s
 3  * @return {number}
 4  */
 5 var titleToNumber = function(s) {
 6     var map = [];
 7     var codeA = "A".charCodeAt(); 
 8     
 9     s = s.toUpperCase();
10     var power = 0;
11     var result = 0;
12     for(var i = s.length - 1; i >= 0; i--, power++){
13         var ch = s[i];
14         result += ( ch.charCodeAt() - codeA + 1 ) * Math.pow(26, power);
15     }
16     
17     return result;
18 };

 

posted @ 2015-05-16 16:03  `Liok  阅读(336)  评论(0编辑  收藏  举报