题目:N Sum

描述: 
Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

样例: 
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9, 
return [0, 1].

解决思路:像这种有关联关系的输入输出,C解决的话要考虑用哈希,Python的话我们直接考虑用字典解决就可以了,遍历数组,并且保存在字典中,查找字典中是否有符合的数字若符合直接返回数组下标和字典的value。 
代码: 
class Solution(object):  #这里我用Python解决
    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

知识点:

range和xrange的区别

>>> a = range(5)
>>> print type (a)
<type 'list'> #我们可以看出range生成的是一个list对象

>>> print range(5)[0] #从list列表中打印list[0]
0
>>> b = xrange(5)
>>> print type(b)
<type 'xrange'>

>>> print xrange(5)[0]#而xrange是一个xrange类型,不会直接生成一个list,而是在每次调用的时候返回其中的一个值,节省内存
0

N Sum的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  10. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. sql 查询强制使用HASH连接性能测试比较

    HASH JOIN 散列连接 hash join是CBO 做大数据集连接时的常用方式.优化器扫描小表(或数据源),利用连接键(也就是根据连接字段计算hash 值)在内存中建立hash表,然后扫描大表, ...

  2. Unity中的Path对应各平台中的Path

    OS: Application.dataPath :                    Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.a ...

  3. ioinc

    ioinc setup sassnpm installionic serve cordova plugin add cordova-plugin-crosswalk-webview 十.开发流程 1. ...

  4. [转载]android的消息处理机制(图+源码分析)——Looper,Handler,Message

    2013-12-18 14:17:33 转载自: http://www.cnblogs.com/codingmyworld/archive/2011/09/14/2174255.html 请跳转到转载 ...

  5. python框架(flask/django/tornado)比较

    一.对外数据接口 三者作为web框架,都是通过url映射对外的接口 flask:以decorator的形式,映射到函数中 django:以字典形式,映射到函数 tornado: 以字典形式,映射到类中 ...

  6. C语言中输入输出函数

    1.1.1 格式化输入输出函数Turbo C2.0 标准库提供了两个控制台格式化输入. 输出函数printf() 和scanf(), 这两个函数可以在标准输入输出设备上以各种不同的格式读写数据.pri ...

  7. 千寻浏览器 1.0 Beta 1(524)(2014年5月27日)

    千寻浏览器--又一款新生浏览器今天进入各位浏览迷的视野.千寻浏览器基于IE内核,据传是由百度浏览器的上海团队操刀,在功能定位上,与目前的QQ浏览器有些相似. 千寻来自官方的解释:寻,追寻,探索,又是古 ...

  8. swift语言之多线程操作和操作队列(上)———坚持51天吃掉大象

    欢迎有兴趣的朋友,参与我的美女同事发起的活动<51天吃掉大象>,该美女真的很疯狂,希望和大家一起坚持51天做一件事情,我加入这个队伍,希望坚持51天每天写一篇技术文章.关注她的微信公众号: ...

  9. 2014年3月份第2周51Aspx源码发布详情

    MVC+EF某钢电子交易平台源码  2014-3-10 [VS2012]功能介绍:本源码是一套完整的电子交易平台系统,完全基于ASP.NET MVC+EF三层构架,开发环境为Visual Studio ...

  10. poj2104 线段树 划分树

    学习:http://www.cnblogs.com/pony1993/archive/2012/07/17/2594544.html 划分树的build: 划分树是分层构建的,在构建的t层时,我们可以 ...