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, and you may not use the same element twice.

Example:                 Given nums = [2, 7, 11, 15], target = 9,               return [0, 1].             Because nums[0] + nums[1] = 2 + 7 = 9

思路:这个题的解决办法有几种方法。

  第一种方法是:暴力破解,对数组中每一种可能进行计算看是否满足要求。 但是这种方法的时间复杂度较高 。时间复杂度为O(n2), 空间复杂度为O(1).

  第二种方法是:使用字典辅助空间,我们先将数组轮询一遍,将数组中的内容存储到字典中, 存储完毕之后,我们将target减去字典中的第一个值得到另外一值看是否存在在字典中,不存在就继续遍历。直到查找到对应结果。时间复杂度为O(n),空间复杂度为O(n)。这种思路还可以将其改进一下我们在遍历存储得过程中边遍历边查找。。如果查找到了就直接返回。否则就继续存储查找。改进之后得时间空间复杂度还是不变。那为什么叫改进呢?因为之前得解决办法,空间复杂度一定为O(n),但是改进之后得最坏得情况空间复杂度为O(n),但这只是个别情况,大部分情况下都不可能存在最坏得情况,都有可能存储了二分之一得空间就找到结果了。所以称其为改进之后得办法。

代码:

  

 class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
if len(nums) < :
return []
tem_dict = {} # 申请辅助空间
for i in range(len(nums)): # 遍历数组
if (target - nums[i]) not in tem_dict: # 查找另外一个数是否在字典中
tem_dict[nums[i]] = i # 不存在就其和下标进行存储。
else:
return [tem_dict[target-nums[i]], i] # 存在的话就返回其下标
return [-, -] # 数组中不存在。

  

												

【LeetCode每天一题】Two Sum(两数之和)的更多相关文章

  1. LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

    1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...

  2. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  3. Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解

    Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全   Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...

  4. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  5. [LeetCode]1.Two Sum 两数之和&&第一次刷题感想

    ---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...

  6. Leetcode题库——1.两数之和

    @author: ZZQ @software: PyCharm @file: addTwoNumbers.py @time: 2018/9/18 10:35 要求:给定两个非空链表来表示两个非负整数. ...

  7. LeetCode 刷题笔记 1. 两数之和(Two Sum)

    tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...

  8. [LeetCode] Two Sum 两数之和

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

  9. leetcode刷题笔记-1. 两数之和(java实现)

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...

随机推荐

  1. Xshell登录Ubuntu12.04

    Ubuntu安装ssh服务: sudo apt-get install openssh-server 打开Xshell,选择“新建”,“连接”设置里选择SSH,主机填入需要连接的主机的IP地址.在“用 ...

  2. go 的文件处理

    准备一个文件 imooc.txt hello world! 一.使用 io/ioutil 包 定义一个 check 函数 func check(err error) { if err != nil { ...

  3. Win 10 计算机管理失效(Windows找不到文件“C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools\Computer Management.lnk)

    Windows找不到文件“C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools\Computer Mana ...

  4. Maven知识点积累一

    配置maven变量,变量名可以是:MAVEN_HOME 或 M2_HOME settings.xml配置本地仓库地址: <localRepository>G:/.m2/repository ...

  5. Entity Framework Core的坑:Skip/Take放在Select之前造成Include的实体全表查询

    今天将一个迁移至 ASP.NET Core 的项目放到一台 Linux 服务器上试运行.站点启动后,浏览器打开一个页面一直处于等待状态.接着奇怪的事情发生了,整个 Linux 服务器响应缓慢,ssh命 ...

  6. *** FATAL ERROR L250: CODE SIZE LIMIT IN RESTRICTED VERSION EXCEEDED

    *** FATAL ERROR L250: CODE SIZE LIMIT IN RESTRICTED VERSION EXCEEDED 在软件已经执行破解仍然出现,是因为工程是破解前建立的,要先执行 ...

  7. Ubuntu下eclipse中运行Hadoop时所需要的JRE与JDK的搭配

    第一组: Eclise 版本:Indigo,Service Release 1 Build id:20110916-0149 Window-->Preferences -->Compile ...

  8. authentication plugin caching_sha2

    操作系统:windows 10 mysql版本:mysql  Ver 8.0.11 for Win64 on x86_64 (MySQL Community Server - GPL) 安装完mysq ...

  9. [No0000DB]C# FtpClientHelper Ftp客户端上传下载重命名 类封装

    using System; using System.Diagnostics; using System.IO; using System.Text; using Shared; namespace ...

  10. React组件中的key

    React组件中的key 一.key的作用 react中的key属性,它是一个特殊的属性,它是出现不是给开发者用的(例如你为一个组件设置key之后不能获取组件的这个key props),而是给reac ...