leetcode打卡系列-2
两数之和
def twosum(nums,target):
for i in range(0,len(nums)):
for j in range(i+1,len(nums)):
if (nums[i]+nums[j]) == target:
return([i,j])
时间复杂度O(N2)
def twosum(nums,target):
dic = {}
for i in range(len(nums)):
j = target - nums[i]
if j in dic.keys():
return [dic[j],i]
dic[nums[i]] = i
时间复杂度O(N)