LeetCode--Excel Sheet Column Number(Excel表格的列名转数字)Python

题目:

给定一个Excel表格的列名,将该列名转为对应数字,样例如下:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28

解题思路:

对字符串进行遍历

代码(Python):

class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        dict0 = {}
        for i in range(26):
            dict0[chr(ord('A')+i)]=i+1

        output = 0
        for i in range(len(s)):
            output = output*26+dict0[s[i]]
        
        return output

你可能感兴趣的:(LeetCode)