LeetCode1

LeetCode1

解法1

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

        dit = {}

        for i in range(len(nums)):

            other = target - nums[i]

            if other not in dit.keys():
                dit[nums[i]] = i

            else:
                return dit[other], i

解法2

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

        dit = {}

        for i in range(len(nums)):
            dit[nums[i]] = i

        for i in range(len(nums)):
            other = target - nums[i]
            if other in dit.keys() and dit[other] != i:
                return dit[other], i

解法3

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

        for i in range(len(nums)-1):
            for j in range(i + 1, len(nums)):
                if nums[i] + nums[j] == target:
                    return i,j
上一篇:大数据开发-从cogroup的实现来看join是宽依赖还是窄依赖


下一篇:C++11标准库thread构造函数浅析