首页 > 代码库 > Leetcode #1. Two Sum

Leetcode #1. Two Sum

key: build and search Hash table.
following up questions: Overflow value for target?
用Hash table记录之间的状态,思想类似DP 


def twoSum(nums, target):
    d = {}
    n = len(nums)
    res = []

    for i in range(n):
        if target - nums[i] in d:
            res = [d[target - nums[i]], i]
        else:
            d[nums[i]] = i

    return res

 

 

 

Leetcode #1. Two Sum