题目来源:

https://leetcode.com/problems/two-sum/


题意分析:

这道题目是输入一个数组和target,要在一个数组中找到两个数字,其和为target,从小到大输出数组中两个数字的位置。题目中假设有且仅有一个答案。


题目思路:

如果直接暴力解决,时间复杂度为(O(n^2)),很明显这种方法会TLE。

那么如果给定的数组是有序的会不会降低难度呢?如果是有序的数组,那么我们可以用“夹逼定理”来处理。简单来说就是首尾相加,如果比target大,则将尾数左移,如果小了首尾右移,直到两个数相加刚好等于target,那么我们可以先将数组排序,然后用“夹逼定理”,这种方法的时间复杂度为(O(nlogn)。这种方法要注意的是排序的时候要记录数组原来的位置,然后再排序。

接下来我来介绍最后一种方法。在python里面有一个dictionary的和C++ 的map功能一样。首先,我们建立一个字典,d = {},字典的key是数组的值num,value是相应的位置, 然后只要满足 num 和 target - num都在字典里面则找到答案。这种方法的时间复杂度是(O(n))。


代码(python):

class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
d = {}# d is a dictionary to map the value of nums and the index in nums
size = 0
while size < len(nums):
if not nums[size] in d:
d[nums[size]] = size + 1 #if nums[size] doesn't exist in d ,create it
if target - nums[size] in d: #if nums[size] and target - nums[size] are both in d
if d[target-nums[size]] < size + 1: # one situation should be minded nums[size] == target - nums[size]
ans = [d[target - nums[size]] , size + 1]# for example [0,1,2] 0 and [0,1,2,0],0
return ans
size = size + 1

PS:注意情况,注意特殊情况。比如target刚好是数组中某个数的2倍,且这个数只有一个或者二个的时候,如[3],6和[3,2,3],6。


转载请说明出处:http://www.cnblogs.com/chruny/

[LeetCode]题解(python):001-Two-Sum的更多相关文章

  1. LeetCode题解之 Continuous Subarray Sum

    1.题目描述 2.循环计算即可 3.代码 bool checkSubarraySum(vector<int>& nums, int k) { ){ return false ; } ...

  2. LeetCode 题解之Minimum Index Sum of Two Lists

    1.题目描述 2.问题分析 直接是用hash table 解决问题 3.代码 vector<string> findRestaurant(vector<string>& ...

  3. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  4. [LeetCode 题解]:Path Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a bi ...

  5. LeetCode题解——Two Sum

    题目地址:https://oj.leetcode.com/problems/two-sum/ Two Sum Given an array of integers, find two numbers ...

  6. LeetCode专题-Python实现之第1题:Two Sum

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. 《LeetBook》LeetCode题解(1) : Two Sum[E]——哈希Map的应用

    001.Two Sum[E] Two SumE 题目 思路 1双重循环 2 排序 3 Hashmap 1.题目 Given an array of integers, return indices o ...

  8. [LeetCode 题解] Combination Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a se ...

  9. [LeetCode 题解]: Two Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

  10. LeetCode 【1】 Two Sum --001

    5月箴言 住进布达拉宫,我是雪域最大的王.流浪在拉萨街头,我是世间最美的情郎.—— 仓央嘉措 从本周起每周研究一个算法,并以swift实现之 001 -- Two Sum (两数之和) 题干英文版: ...

随机推荐

  1. The Swift Programming Language--语言指南--协议

    Protocol(协议)用于统一方法和属性的名称,而不实现任何功能.协议能够被类,枚举,结构体实现,满足协议要求的类,枚举,结构体被称为协议的遵循者.   遵循者需要提供协议指定的成员,如属性,方法, ...

  2. Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值

    一.第一个界面 // Created by 秦志伟 on 14-6-13. import UIKit class ZWRootViewController: UIViewController { in ...

  3. Response.Write具体介绍

    问题一: Response.Write 后连接Response.Redirect ,则Response.Write无法显示,直接跳转入Response.Redirect 的页面. 解决方案: Resp ...

  4. VBA 开发学习--基础语法2

    VBA中的运算符 算数运算符及其作用 + 求两个数的和 -  求两个数的差:求一个数的相反数 *  求两个数的积 /   求两个数的商(保留小数位   5/2=2.5) \   整除 (保留整数位  ...

  5. 微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]

    由于这周比较忙,所以本来想做的性能测试,一直没时间,想想还是今天给补上吧 由于很多人都担心性能问题,封装之后跟Dapper的性能差距是多少,今天我给出我的测试方法,仅供参考. 创建IDbConnect ...

  6. 5分种让你了解javascript异步编程的前世今生,从onclick到await/async

      javascript与异步编程 为了避免资源管理等复杂性的问题,javascript被设计为单线程的语言,即使有了html5 worker,也不能直接访问dom. javascript 设计之初是 ...

  7. VC++学习之GDI概述

    VC++学习之GDI概述 图形设备接口(GDI)是一个可执行程序,它接受Windows应用程序的绘图请求(表现为GDI函数调用),并将它们传给相应的设备驱动程序,完成特定于硬件的输出,象打印机输出和屏 ...

  8. Python: how to public a model

    1, Create a folder fileFolder 2, create a file tester.py 3, create another file setup.py: The conten ...

  9. 【转】jsp 表单form传值

    写的很好,看到了忍不住不转啊,希望可以分享一下~~ 转载自http://blog.csdn.net/anmei2010/article/details/4140216 页面间链接和数据传递的三种方式 ...

  10. Paragraph Vector在Gensim和Tensorflow上的编写以及应用

    上一期讨论了Tensorflow以及Gensim的Word2Vec模型的建设以及对比.这一期,我们来看一看Mikolov的另一个模型,即Paragraph Vector模型.目前,Mikolov以及B ...