1.两数之和

解法1:

1 class Solution:
2        def twoSum(self, nums: List[int], target: int) -> List[int]:
3            for i in nums:
4                j = target - i
5                start_index = nums.index(i)
6                next_index = start_index + 1
7                temp_nums = nums[next_index: ]
8                if j in temp_nums:
9                   return nums.index(i), next_index + temp_nums.index(j)

解法2:

1 class Solution:
2     def twoSum(self, nums: List[int], target: int) -> List[int]:
3         dict = {}
4         for i in range(len(nums)):
5             if target - nums[i] not in dict:
6                 dict[nums[i]]  = i
7             else:
8                 return [dict[target - nums[i]], i]

 

posted @ 2019-07-11 16:09  我叫郑小白  阅读(237)  评论(0编辑  收藏  举报