题目链接: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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. linux 文件结构

    Linux常见的目录解释: 目录 描述 / 根目录 /bin 做为基础系统所需要的最基础的命令就是放在这里.比如 ls.cp.mkdir等命令:功能和/usr/bin类似,这个目录中的文件都是可执行的 ...

  2. 短URL链接系统

    定义: 短网址(Short URL),顾名思义就是在形式上比较短的网址.但不知道有多少人像我一样,由于面试问道才知道有这种系统而对短连接原理好奇,从而进行进一步的研究.在Web 2.0的今天,不得不说 ...

  3. 为什么KVM计算机点无故重启?

    一.故障1:机器hangs 本地一台cloudstack计算节点无故连不上了,cloudstack也坏了,后查看有一台系统虚拟机在这台计算节点上,导致cs挂了.去找到这台机器后,发现这台机器卡住了,重 ...

  4. Vue语法

    第一个Vue示例: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  5. Springmvc ajax请求400

    转载做记录 传JSON对象 前端 function test () { var param = {username : "yitop"}; $.ajax({ timeout : 2 ...

  6. Tkinter Checkbutton

    Python - Tkinter Checkbutton: checkbutton小部件用于显示切换按钮的用户多项选择.然后,用户可以通过点击相应的按钮每个选项中选择一个或多个选项.   checkb ...

  7. linux中find工具

    find 由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下.即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只要你具有相应的权限. ...

  8. 说说JDK中的List-ArrayList、Vector、LinkedList

    为方便开发人员,JDK提供了一套主要数据结构的实现,比如List.Map等.今儿说说List接口. List接口的一些列实现中,最常用最重要的就是这三个:ArrayList.Vector.Linked ...

  9. VUE 初步学习

    Vue 简单的总结一 Vue 简单的总结二 Vue 简单的总结三 Vue 简单的总结四(项目流程) Vue 简单的总结五 Vue(6)- Vue-router进阶.单页面应用(SPA)带来的问题 Vu ...

  10. 【iBatis】使用resultMap配置返回结果时报错“列名无效”

    使用iBatis联查DEPT.EMP两张表时 采用如下结构 <resultMap id="find_departmentMap" class="com.dto.De ...