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 ...
随机推荐
- 【转】利用JMeter进行压力测试
压力测试以软件响应速度为测试目标,尤其是在较短时间内大量并发用户的同时访问时,软件的性能和抗压能力. JMeter是一款开源的压力测试工具,目前最新Release版本是2.3.4,它不仅可以测试Web ...
- Html5 input日期时间输入类型标签赋值 正确的格式应该是value="2014-03-15T08:00"
自己也亲测了!T在php的date函数中格式化后是CST 所以不能写成date('Y-m-dTH:i:s',time()) 得把把年月日和时间用date函数分别格式化 再用大写T分割开 放在v ...
- APP推送通知相关实现
关于推送通知,iOS推送主要是通过服务端来实现的,相关过程可以参考下面两篇文章: http://cshbbrain.iteye.com/blog/1859810 http://zxs198 ...
- 关于v$BH
关于v$bh的相关字段值FILE# NUMBER Datafile identifier number (to find the filename, query DBA_DATA_FILES or V ...
- 4_python之路之模拟工资管理系统
python之路之模拟工资管理系统 1.程序说明:Readme.txt 1.程序文件:salary_management.py info.txt 2.程序文件说明:salary_management. ...
- oracle用户具有的权限和角色
如何查看一个oracle用户具有的权限和角色 1.查看所有用户: select * from dba_users; select * from all_users; select * from use ...
- C投票系统
- 推荐一篇mysql优化干货
淘宝的技术一直比较前沿,特别是LVS的作者加入淘宝后,淘宝和阿里的开源做的有声有色,君不见淘宝出了tengine,tairs,tddl,hsf(soa框架,未开源),tfs(小文件存储系统)等等,阿里 ...
- [js方法pk]之instanceof() vs isPrototypeOf() hasOwnProperty() vs propertyIsEnumerable()
这几个方法在js的高级编程中经常用到,对于新手来说可能还不知道他们有什么区别,我把我的体会总结下来,供大家参考: 首先,定义一个对象: function Parent() {this.name = & ...
- pipenv 简要指南
pipenv 简要指南 pipenv是requests作者的一个项目, 整合了virtualenv, pip, pipfile, 用于更方便地为项目建立虚拟环境并管理虚拟环境中的第三方模块. 安装 直 ...