Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 573 Bytes

File metadata and controls

25 lines (21 loc) · 573 Bytes

两层遍历

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        length = len(nums)
        for i in range(length - 1):
            for j in range(i + 1, length):
                if nums[i] + nums[j] == target:
                    return i, j
        return None

Hash

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        dct = {}
        for i, n in enumerate(nums):
            if target - n in dct:
                return [dct[target - n], i]
            dct[n] = i