Leetcode#171. Excel Sheet Column Number(Excel表列号--进制转换)
2018-01-27 17:34
399 查看
题目
Related to question Excel Sheet Column TitleGiven 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进制Python语言
class Solution(object): def titleToNumber(self, s): """ :type s: str :rtype: int """ temp="0ABCDEFGHIJKLMNOPQRSTUVWXYZ"; n = 0; for i in range(0, len(s)): n = n * 26 + temp.index(s[i]); return n;
C++语言
class Solution { public: int titleToNumber(string s) { string temp="0ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int n = 0; for(int i=0; i<s.length(); i++) { n = n * 26 + temp.find(s[i]); } return n; } };
相关文章推荐