class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
hashTable = {}
for i in range(len(nums)):
hashTable[nums[i]] = i
for i in range(len(nums)):
if target  nums[i] in hashTable and i != hashTable[target  nums[i]]:
return [i, hashTable[targetnums[i]]]
return []
Python solution


There are two things you can avoid here:
 Passing on the array more than once
 Checking if the number you found is not your current number (the right side of the
and
)
class Solution(object): def twoSum(self, nums, target): h = {} for i, num in enumerate(nums): if (target  num) in h: return [i, h[target  num]] h[num] = i