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 ...
随机推荐
- 【SQLYOG】SSH ERROR:UNABLE TO OPEN CONNECTION:GETHOSTBYNAME:UNKNOWN ERROR牵引出来的一系列问题
出现这个问题很蹊跷,SQLyog管理过一二十台的mysql服务器或者vps,连接一直没有问题,各种服务商的都没问题,也包括阿里云的.可昨天偏偏一台阿里云的服务器本地通过SQLyog去连接它的时候报这样 ...
- erlang的lists笔记
一般循环用在遍历列表的时候,erlang有lists模块直接支持遍历,不需要自己写尾递归遍历list lists:foreach 用来遍历列表,不保存结果,最后一次返回ok lists:map 遍历列 ...
- 基于标准库的string类实现简单的字符串替换
感觉基本功还是不扎实,虽然能做些程序但是现在看来我还是个初学者(primer),试着完成习题结果还得修修改改. 废话不多说,实现功能很简单,<C++ Primer>9.5.2节习题. // ...
- mstsc遇到CredSSP加密Oracle修正
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\P ...
- cordova 安装使用
前人总结: Cordova是Apache软件基金会的一个产品.其前身是PhoneGap,由Nitobi开发,2011年10月,Adobe收够了Nitobi,并且PhoneGap项目也被贡献给Apach ...
- java代码--实现随机输出10个随机数,并显示最大值,最小值
总结;对于length()属性,还不是很熟悉.不会用它. package com.s.x; //随机产生10个随机数,并且显示出最大值,最小值 public class Love { public s ...
- 网络 、osi 七层模型、tcp/ip 五层参考
网络 网络的本质就是通讯,比特传输 网络拓扑 物理布局pc -- 交换机 -- 路由器逻辑布局pc -- 路由器 交换机的产生 网络之初,是通过网线互相连通到各个主机,存在的问题就是2个pc都要与服务 ...
- MATLAB常用方法技巧总结
===================================================================================================M ...
- 03-22 Ajax验证用户登录
在网页中一般是通过表单提交数据,而表单获取信息,抛弃当前页面重新加载一个新页面. 现在,在webform网页中可以通过JueryAjax提交.处理数据的方式,达到异步刷新页面. 表单提交数据和Juer ...
- My97DatePicker日期控件的使用
本文演示如何在MyEclipse项目中使用My97DatePicker日期控件 1.下载My97DatePicker日期控件, My97DatePicker日期控件下载地址 2.在MyEclipse项 ...