leetCode--towSum
题目链接:https://leetcode.com/problems/two-sum/description/
此题的意思是:给定一个target值,从给定的数组中找两个数,使得这两个数的和==target。要求同一个数不允许使用两次
注意:可能会有负整数
看到题时的思路:
1、进行两层循环进行暴力查找,时间复杂度为O(n2),但是oj上的题这样做肯定超时。pass
2、对数组进行排序,然后左右夹逼,这样时间复杂度为O(nlogn),但是这样无法返回下标,因为一旦排序,下标就会被打乱。pass
int[] res = new res[2]; //存放结果
for(int i = 0;i<nums.length;i++){
for(int j = nums.length-1;i>0;i--){
if(nums[i]+nums[j]<target) break;
if(nums[i]+nums[j]==target){
if(i==j) break;
if(i!=j){
res[0]=nums[i]; //无法返回下标,只能返回对应的值
res[1]=nums[j];
}
}
}
}
3、遍历一遍整个数组,用另一个数组记录下flag[i]=target-nums[i]的值,然后找到flag[i]与nums[i]中相等的值,但是这样做还是需要双重循环,时间复杂度O(n2)。pass
4、求救百度
百度上的做法几乎都是
(1)先用一层循环使用Map来对nums[i]中数据进行键值映射,m.put(nums[i],i)
(2)再用一层循环,令flag = target - nums[i] 此时使用map.containsKey(flag)函数来判断flag值是否存在
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
int[] res = new int[2];
for(int i = 0;i<nums.length;i++){
m.put(nums[i],i);
}
for(int i = 0;i<nums.length;i++){
int flag = target - nums[i];
//
if(m.containsKey(flag)&&m.get(flag)!=i){
res[0] = i;
res[1] = m.get(flag);
break;
}
}
return res;
}
}
虽然这样做可以通过,但是我们不能知其然而不知其所以然,因此就需要深入的了解下m.containsKey()函数为什么时间复杂度比较低
通过m.containsKey()的源码可以发现,返回的是一个布尔值,即如果当前的key值存在返回true否则返回false。此方法是通过调用getNode()实现的,

我们继续进入getNode(),说实话我看的有点懵逼,大致的意思就是说:通过数组的长度与key的hash值进行与运算取到key值的下标,时间复杂度为O(1)。此处参考文章 : https://www.jianshu.com/p/06e2be54d4d6

leetCode--towSum的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- golang的beego框架开发时出现的问题纪录
golang的beego框架开发时出现的问题纪录1.数据库并发时问题:[ORM]2017/02/20 23:44:05 -[Queries/default] - [FAIL / db.Query / ...
- 雅虎CSS初始化代码
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,b ...
- C#操作MySql数据库帮助类(Dapper,T-Sql)
using System.Text; using MySql.Data.MySqlClient; using System.Data; using Dapper; using System.Refle ...
- python 生成器和生成器表达式
1.生成器 生成器的本质就是迭代器 生成器的特点和迭代器一样.取值方式和迭代器一样(__next__(),send():给上一个yield传值) 生成器一般由生成器函数或者生成器表达式来创建 其实就是 ...
- xss 攻击 sql 注入
XSS测试 "/><script>alert(document.cookie)</script><!-- <script>alert(docu ...
- zookeeper基本讲解(Java版,真心不错)
1. 概述 Zookeeper是Hadoop的一个子项目,它是分布式系统中的协调系统,可提供的服务主要有:配置服务.名字服务.分布式同步.组服务等. 它有如下的一些特点: 简单 Zookeeper的核 ...
- buffer cache 深度解析
本文首先详细介绍了oracle中buffer cache的概念以及所包含的内存结构.然后结合各个后台进程(包括DBWRn.CKPT.LGWR等)深入介绍了oracle对于buffer cache的管理 ...
- Linux更换jdk版本的一些问题
111111111111111111111111111111111111111111111111111111111111111111111111 在服务器上更新了新的 jdk,也在 /etc/prof ...
- Centos下Apache+Tomcat集群--搭建记录
一.目的 利用apache的mod_jk模块,实现tomcat集群服务器的负载均衡以及会话复制,这里用到了<Cluster>. 二.环境 1.基础:3台主机,系统Centos6.5,4G内 ...
- windows server 2008 配置DNS服务器与IIS
0x00: 总结这个星期在学校学的. 0x01安装: 首先你得安装好windows server 2008 然后在添加角色->安装IIS和DNS服务器 勾选好你要安装的. 安装-> 根据老 ...