Starting the journey
N2I -2020.03.15
1. My first solution
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dic={}
for index,item in enumerate(nums):
dic[item]=index
for index,item in enumerate(nums):
temp=target-item
if temp in dic and dic[temp]>index:
return [index,dic[temp]]
2. Other Answers
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
memo = dict()
for i, n in enumerate(nums):
if n in memo:
return [memo[n], i]
memo[target-n] = i
(Close to the first answer)
Comments
Post a Comment