码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode][JavaScript]Excel Sheet Column Number

时间:2015-05-16 18:13:39      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

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 




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

 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 };

 

[LeetCode][JavaScript]Excel Sheet Column Number

标签:

原文地址:http://www.cnblogs.com/Liok3187/p/4507967.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!