[LeetCode] 1. Two Sum

__author__ = 'zenglinwang'

class Solution(object):

    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """

        map = dict()

        # Since two numbers are mutually complemented, one pass hash table can do.
        for i in range(0, len(nums)):
            complement = target - nums[i]
            # print "- [{0:d},{1:d}]".format(nums[i], i)
            # print map.items()
            if complement in map.keys() and map.get(complement) != i:
                # print "[{0:d},{1:d}]".format(map.get(complement), i)
                return [map.get(complement), i]
            map[nums[i]] = i

# s = Solution()
# s.twoSum([0,4,3,4,5,4], 8)

 

posted @ 2016-08-05 10:49  兔纸不吃草  阅读(122)  评论(0编辑  收藏  举报