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

 

 

 
posted @ 2016-12-13 03:38  lettuan  阅读(93)  评论(0编辑  收藏  举报