美文网首页
LeetCode1:Two Sum

LeetCode1:Two Sum

作者: everfight | 来源:发表于2017-03-22 13:46 被阅读14次
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

    问题:Given an array of integers, returnindicesof the two nu...

  • LeetCode1:Two Sum

    注:最精华的思想是:x = nums[i] dict[x] = i取出第i个数字(以0为开头),把它的值装载...

  • leetcode1 Two Sum

    题目 https://leetcode.com/problems/two-sum/ 思路 盲猜N^2的遍历会超时,...

  • two_sum leetcode1

    题目的简介 Given an array of integers, return indices of the t...

  • X Sums

    Two Sum It is not easy to write Two-Sum right for the fir...

  • Leetcode 解题记录

    Two sum From https://leetcode.com/problems/two-sum/descri...

  • #1. Two Sum & 167 Two Sum II

    Two Sum I https://leetcode.com/problems/two-sum/#/descrip...

  • 1. Two Sum

    1. Two Sum 题目:https://leetcode.com/problems/two-sum/ 难度: ...

  • leetcode hot100

    1. Two Sum[https://leetcode-cn.com/problems/two-sum/] 字典(...

  • LeetCode 1. Two Sum (Easy)

    LeetCode 1. Two Sum (Easy) LeetCode 1. Two Sum (Easy) 原题链...

网友评论

      本文标题:LeetCode1:Two Sum

      本文链接:https://www.haomeiwen.com/subject/nfzknttx.html