leetcode-5.2[171. Excel表列序號、168. Excel表列名稱、167. 兩數之和 II - 輸入有序數組](python實現)

題目1

在這裏插入圖片描述

題解1

class Solution:
    def titleToNumber(self, s: str) -> int:
        res = 0
        for index, word in enumerate(s, start=1):
            if index < len(s):
                res = (res + ord(word)-64)*26
            else:
                res += ord(word)-64
        return res
    

附上題目鏈接

題目2

在這裏插入圖片描述

題解2

class Solution:
    def convertToTitle(self, n: int) -> str:
        tmp = {1: 'A',2: 'B',3: 'C',4: 'D',5: 'E',6: 'F',7: 'G',8: 'H',9: 'I',
               10: 'J',11: 'K',12: 'L',13: 'M',14: 'N',15: 'O',16: 'P',17: 'Q',
               19: 'S',20: 'T',21: 'U',22: 'V',23: 'W',24: 'X',25: 'Y',0: 'Z',
               }
        res=''
        while n>0:
            res+=tmp[n%26]
            if n%26:
                n=n//26
            else:
                n=(n-26)//26
        return res[::-1]

附上題目鏈接

題目3

在這裏插入圖片描述

題解3

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        """
            雙指針法
        """
        n = len(numbers)
        if n <= 1:
            return
        l, r = 0, n-1
        while l < r:
            if numbers[l]+numbers[r] < target:
                l += 1
            elif numbers[l] + numbers[r] > target:
                r -= 1
            else:
                return [l+1,r+1]

附上題目鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章