您的位置:首页 > 其它

Leetcode#171. Excel Sheet Column Number(Excel表列号--进制转换)

2018-01-27 17:34 399 查看

题目

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进制

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: