1. 程式人生 > >leetcode 兩數之和 python

leetcode 兩數之和 python

class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ dic = {} # 開一個雜湊表 for i in range(len(nums)): if target - nums[i] in dic: # 如果另一個數之前遍歷過 在哈系裡 就返回 return
[dic[target-nums[i]], i] dic[nums[i]] = i # 把當前這個數和位置放入雜湊