leetcode刷題--Excel Sheet Column Number

黃思喆發表於2015-05-18

題目:給出一個excel的列編號,求出對應的數字

這道題和上一題差不多,就是倆過程翻過來

思路可以是這樣,dict存放對映,然後對應成數字再計算

程式碼:

class Solution:
    # @param {string} s
    # @return {integer}
    def titleToNumber(self, s):
        fix=ord("A")
        n=len(s)
        result=0
        map={"A":1,"B":2,"C":3,"D":4,"E":5,"F":6,"G":7,"H":8,"I":9,"J":10,"K":11,"L":12,"M":13,"N":14,"O":15,"P":16,"Q":17,"R":18,"S":19,"T":20,"U":21,"V":22,"W":23,"X":24,"Y":25,"Z":26,}
        for i in range(n):
            result+=(26**(n-i-1))*map[s[i]]

        return result 

相關文章