算法1
(hash) $O(n^2)$
使用hash,用python中的dic实现。
时间复杂度分析:O(n)
python代码
class Solution(object):
def findNumbersWithSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
dic = {}
for num in nums:
if target-num in dic:
return [target-num ,num]
else:
dic[num] = 1