方法一:
class Solution(object):
def twoSum(self,nums,target):
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i] + nums[j] == target:
return i,j
这个方法能运行27个测试用例,到第28个,测试失败,原因当nums数量大的时候,程序运行超时。
方法二:
class Solution(object):
def twoSum(self,nums,target):
for i,value in enumerate(nums):
a = target - value
if a in nums:
b = nums.index(a)
if i != b:
return i,b