首页 > 其他 > 详细

[LeetCode] 1. Two Sum

时间:2016-08-05 11:28:05      阅读:90      评论:0      收藏:0      [点我收藏+]
__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)

 

[LeetCode] 1. Two Sum

原文:http://www.cnblogs.com/skyssky/p/5740405.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!