class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
dic = {}
mytarget = 0
result = []
for i in range(len(nums)):
dic[nums[i]] = i
for i in range(len(nums)):
mytarget = target - nums[i]
if mytarget in dic and dic[mytarget]!=i:
result.append(i)
result.append(dic[mytarget])
break
return result
利用哈希表的查找时间快而实现的解题优化