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

 

26进制转10进制,注意以'A'而不是0开头,因此要“+1”

 

C++:

 1 class Solution {
 2 public:
 3     int titleToNumber(string s) {
 4         int num=0;
 5         for(int i=0;i<s.length();i++)
 6             num=num+pow(26,i)*(s[s.length()-i-1]-64);
 7             
 8         return num;
 9     }
10 };

 

Python:

 1 class Solution:
 2     # @param s, a string
 3     # @return an integer
 4     def titleToNumber(self, s):
 5         num=0
 6         for i in range(0,len(s)):
 7             num=num+pow(26,i)*(ord(s[len(s)-i-1])-64)
 8         return num
 9 
10 //ord与chr作用相反
11 //ord 字符转整型
12 //chr 整型转字符

 

posted on 2015-04-05 18:15  黄瓜小肥皂  阅读(153)  评论(0编辑  收藏  举报