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. 凭据管理 API

    api 有相应更新 https://www.chromestatus.com/features/4781762488041472 <!DOCTYPE html> <html> ...

  2. 源码安装git工具,显示/usr/local/lib64/libcrypto.a(dso_dlfcn.o) undefined reference to `dlopen'

    /usr/local/lib64/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':dso_dlfcn.c:(.text+0x30) ...

  3. 使用 Markdown 写技术博客,踩过的 6个坑

    目录 Markdown 特性 Markdown 简介 常用语法 为什么流行 设计哲学 工具支持 版本演进 标准化之路 踩过了坑 平台帮助文档 语法差异 显示效果 我的最佳实践 摘要:本文记录我在使用 ...

  4. Apache + PHP配置

    因工作需要,重新配置了Apache和PHP.想起当年曾经配置过,但是已经忘得差不多了.而且,也没有记录.从我个人来看,确实缺乏这样的训练,从国家教育体系来看,似乎也从未有过做科学记录的训练.中国的瓷器 ...

  5. 用CSS画基本图形

    用CSS画基本图形 1.正方形 代码如下: #square { width: 100px; height: 100px; background: red; } 2.长方形 代码如下:   #recta ...

  6. spark运行wordcount程序

    首先提一下spark rdd的五大核心特性: 1.rdd由一系列的分片组成,比如说128m一片,类似于hadoop中的split2.每一个分区都有一个函数去迭代/运行/计算3.一系列的依赖,比如:rd ...

  7. linux fix the superblock by dumpe2fs fsck

    It seems that you have a bad superblock. To fix this: Firstly, boot into a live CD or USB Find out y ...

  8. [dpdk] dpdk多线程任务调度

    DPDK下的线程,叫做EAL线程. EAL线程默认是与CPU core一对一绑定的,这样的话,有一些实时性,计算量不高的任务独占CORE是一种浪费,大概找了如下几种解决方案. 1. dpdk seri ...

  9. Python中给List添加元素的4种方法

    https://blog.csdn.net/hanshanyeyu/article/details/78839266 List 是 Python 中常用的数据类型,它一个有序集合,即其中的元素始终保持 ...

  10. 转:java内部类作用

    原文地址:https://www.cnblogs.com/uu5666/p/8185061.html 一. 定义 放在一个类的内部的类我们就叫内部类. 二. 作用 1.内部类可以很好的实现隐藏, 一般 ...