方法一:
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
t=-1
q=0
for i in nums:
t=t+1
q=t+1
for j in nums[q:]:
if i+j==target:
return [t,q]
q=q+1;
方法二:
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
n = len(nums)
d = {}
for x in range(n):
a = target - nums[x]
if nums[x] in d:
return d[nums[x]],x
else:
d[a] = x