題目來源自leetcode與代碼随想錄
(1)1.雙數之和 萬物起源
題目描述:
給定一個整數數組 nums 和一個目标值 target,請你在該數組中找出和為目标值的那 兩個 整數,并傳回他們的數組下标
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashMap = dict()
for i,val in enumerate(nums):
if target-val in hashMap:
return [ hashMap[target-val], i ]
hashMap[nums[i]] = i
return []