首页 > 其他 > 详细

1. Two Sum

时间:2017-05-22 13:08:07      阅读:306      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/articles/two-sum/#approach-2-two-pass-hash-table-accepted

 

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        seen = {} # use dictionary data structure
        for i in range(len(nums)): # i is the position/key of the num
            num2 = target - nums[i]  # num2 is the value
            if num2 in seen: # num2 is the key
                return [seen[num2], i]
            else:
                seen[nums[i]] = i
        return Not found

 

1. Two Sum

原文:http://www.cnblogs.com/prmlab/p/6888767.html

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