LeetCode1:Two Sum

class Solution(object):
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            dict = {}
            for i in xrange(len(nums)):
                x = nums[i]
                if target-x in dict:
                    return (dict[target-x], i)
                dict[x] = i

注:
最精华的思想是:
x = nums[i] dict[x] = i
取出第i个数字(以0为开头),把它的值装载进dict中成为key,而原来的序号i变成了value
最终就能够轻松的取出,value的值,即数列中的序号。

你可能感兴趣的:(LeetCode1:Two Sum)