码迷,mamicode.com
首页 > 其他好文 > 详细

1. Two Sum

时间:2017-05-22 13:42:21      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:.com   int   ash   ted   val   range   ret   hash   sum   

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

标签:.com   int   ash   ted   val   range   ret   hash   sum   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!