【LeetCode每天一题】Two 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, 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(两数之和)的更多相关文章
- 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 ...
- 【LeetCode】1. Two Sum 两数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全 Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- [LeetCode]1.Two Sum 两数之和&&第一次刷题感想
---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...
- Leetcode题库——1.两数之和
@author: ZZQ @software: PyCharm @file: addTwoNumbers.py @time: 2018/9/18 10:35 要求:给定两个非空链表来表示两个非负整数. ...
- LeetCode 刷题笔记 1. 两数之和(Two Sum)
tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...
- [LeetCode] Two Sum 两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode刷题笔记-1. 两数之和(java实现)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...
随机推荐
- Django之Web框架本质及第一个Django实例
Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 半成品自定义web框架 impor ...
- Solr-全文检索工具简介
一.Solr的简介 Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务.Solr可以独立运行在Jetty.Tomcat等这些Servlet容器中.都是W ...
- 在服务器上搭建git仓库
文档 管理全部用户的公匙 /tmp/id_rsa.ajanuw.pub // 这里全部放在 /tmp目录下 在服务器上创建一个名叫 git 的用户 adduser git // 一路回车 passwd ...
- Spark RDD 默认分区数量 - repartitions和coalesce异同
RDD.getNumPartitions()方法可以获得一个RDD分区数量, 1.默认由文件读取的话,本地文件会进行shuffle,hdfs文件默认会按照dfs分片来设定. 2.计算生成后,默认会按照 ...
- sshpass 配置密码登录ssh
tar -zxvf sshpass-1.06.tar.gzcd sshpass-1.06./configuremake && make install sshpass -p userp ...
- ThinkPHP框架 AJAX方法返回 AJAX实现分页例子:
在模块控制器Controller文件夹里创建一个 FenyeController.class.php控制器 <?php namespace Admin\Controller; use Think ...
- Ubuntu将网卡名称eno160改为eth0并且设置静态IP
修改配置文件/etc/default/grub GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0" 设置生效 update-grub ...
- BZOJ 1500/Luogu 2042 - 维修数列 - [NOI2005][Splay]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1500 题目链接:https://www.luogu.org/problemnew/sho ...
- 批量kill java进程方法-引出子shell和反引用
方法: kill –9 `pgrep java` 使用上述命令可以将服务器上运行的所有java进程一次性kill掉. 扩展:子shell和反应用在shell脚本中的作用 先来看一个子shell的例子: ...
- Python:多线程
据廖雪峰老师的学习文档介绍,高级语言通常都内置多线程的支持,Python也不例外,并且,Python的线程是真正的Posix Thread,而不是模拟出来的线程. Python的标准库提供了两个模块: ...