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 ...
随机推荐
- jeecg中树形显示的用法
1.GoodsController中显示的方法如下: @RequestMapping(params = "goodsgrid") @ResponseBody public Obje ...
- OPC UA (统一架构)的优势
OPC UA OPC统一架构(OPC Unified Architecture)是OPC基金会(OPC Foundation)创建的新技术,更加安全.可靠.中性(与供应商无关),为制造现场到生产计划或 ...
- cf Double Happiness(判断是否为素数且为4k+1型)
2790. Double Happiness time limit per test 3 seconds memory limit per test 128 megabytes input sta ...
- java 的一个hellow word 代码解释
/* This is a simple Java program. Call this file "Example.java". */(上面是注释的方法) class Exampl ...
- AD域中客户端时间与服务器同步
1.域控配置 修改注册表,设置域控服务器名称 设置组策略,启动NTP服务器 域策略中设置windows time服务自动启动 2.客户端 更新域策略gpupdate /force 如果不重启的话,先n ...
- ngui自适应
增加UIROOT using UnityEngine; namespace Com.Xyz.UI { [ExecuteInEditMode] [RequireComponent(typeof(UIRo ...
- python学习(二十) Python 中的比较:is 与 ==
Python 中的比较:is 与 == 在 Python 中会用到对象之间比较,可以用 ==,也可以用 is .但是它们的区别是什么呢? is 比较的是两个实例对象是不是完全相同,它们是不是同一个对象 ...
- HDU1257题解
解题思路:这题的本质就是:求一个给定的数字序列中,非递增(或非递减)子序列的最少的条数. 思维误区:本题很容易被样例坑,以为是直接求跳跃点(i < j && ai < aj ...
- js控制电池
js控制电池 判断设备是否在充电 navigator.getBattery().then(function(battery){ if(battery.charging) { alert("电 ...
- cocos2dx切换播放的动画
版本:cocos2dx 2.2.6 IDE: VS2012 语言:C++98 美术资源一共有两段动画的序列帧,一个是手绘马行走图,一个是分子人行走图. 程序要实现的目的就是在同一个位置,点击按钮可以实 ...