leetcode练习 two sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

因为nums和target都是由系统设定的,我们只是在上面编写需要的程序。初学者建议直接注册一个LeetCode的账号,直接在上面刷题,这样方便
与正确答案进行对比。这里是用Python3语言进行的编程。
1 class Solution: 2 def twoSum(self, nums, target): 3 """ 4 :type nums: List[int] 5 :type target: int 6 :rtype: List[int] 7 """ 8 dict = {} 9 for i in range(len(nums)): 10 if nums[i] in dict: 11 return [dict[nums[i]], i] 12 else: 13 dict[target - nums[i]] = i
浙公网安备 33010602011771号