题目

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

解法一

思路

看到这道题,我的第一个思路就是用两个for循环来解,这样的话时间复杂度为O(n2),解法很简单,但是不是最优解。

代码

class Solution {public int[] twoSum(int[] nums, int target) {int[] result = new int[2];A:for(int i = 0; i < nums.length; i++) {for(int j = i+1; j < nums.length; j++) {if(nums[i] + nums[j] == target) {result[0] = i;result[1] = j;break A;}}}return result;}
}

解法二

思路

认真看了讨论区,学习到了用HashMap来解题,由于对HashMap不是很熟悉,所以去学习了HashMap的相关知识,发现用HashMap的查找时间复杂度接近为O(1),所以现在我们只用一个for循环就可以实现算法,时间复杂度接近O(n)。

class Solution {public int[] twoSum(int[] nums, int target) {int[] result = new int[2];HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();for(int i = 0; i < nums.length; i++) {if(hash.containsKey(target - nums[i])) {result[0] = hash.get(target - nums[i]);result[1] = i;    //这里注意result[0]和result[1]的值不要写反break;}hash.put(nums[i], i);}return result;}
}

补充:HashMap的相关知识

HashMap底层是通过数组加链表来实现的。如下图所示:

当查询一个键值对的时候,实际上一共进行了四步:
1.先根据key值计算出hash值,然后经过h&(length-1)得到index值
2.查找数组中的index位置,得到相应位置的键值对表
3.根据key值,遍历链表,找到相应的键值对。
4.从键值对中取出value值。

其中1,2,4的时间复杂度均为O(1),而3只在理想情况下可以在O(1)时间下完成,“冲突”越少,即数组每个index的链表越短,查找效率就越快。

更多的HashMap底层的相关知识可参考:Java集合:HashMap源码剖析

转载于:https://www.cnblogs.com/shinjia/p/9717174.html

[leetcode]1.Two Sum相关推荐

  1. leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)

    题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...

  2. 【动态规划】LeetCode 377. Combination Sum IV

    LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...

  3. 【DFS】LeetCode 39. Combination Sum

    LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...

  4. LeetCode 167.Two Sum II 解题报告

    LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...

  5. leetcode 1005 Maximize Sum Of Array After K Negations

    leetcode 1005 Maximize Sum Of Array After K Negations 1.题目描述 2.解题思路 3.Python代码 1.题目描述 给定一个整数数组 A,我们只 ...

  6. [勇者闯LeetCode] 1. Two Sum

    [勇者闯LeetCode] 1. Two Sum Description Given an array of integers, return indices of the two numbers s ...

  7. [勇者闯LeetCode] 112. Path Sum

    [勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...

  8. 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...

  9. LeetCode 653. Two Sum IV - Input is a BST--Python解法

    题目地址:Two Sum IV - Input is a BST - LeetCode Given a Binary Search Tree and a target number, return t ...

  10. LeetCode 167. Two Sum II - Input array is sorted--Python解法

    题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...

最新文章

  1. 数据分析工具Pandas(4):Pandas的函数应用
  2. js 删除数组几种方法
  3. SCRIPT LOAD lua文件
  4. Jedis干什么用的
  5. 【学术相关】是什么让女性在计算机史上“隐身”了?
  6. shell查找命令大全
  7. base32解码工具_[随波逐流]CTF编码工具 V1.0
  8. 斯坦福大学博士后王鸿伟: 知识图谱辅助的个性化推荐系统
  9. 数学建模——sas(1)——几种统计方法
  10. 网站压力测试的几种方法
  11. Vue中如何解决跨域问题
  12. android tv 国内使用,不花钱解决 Android TV 原生系统国内APP不显示图标
  13. 必须了解的网络运维知识
  14. 生鲜配送ERP系统_对商品模块数据模型与界面设计的思考【Java 开源版】杭州生鲜配送系统_升鲜宝_SaaS全链路生鲜供应链管理系统_升鲜宝
  15. 婚介行业线上引流渠道哪些?你还在为找不到客户而烦恼吗?这些渠道千万别错过!
  16. DWZ的几个常用组件
  17. 冷笑话 企鹅与北极熊
  18. Linux实用的快捷键
  19. 机器视觉实验四: 为人脸添加装饰物特效实验(OpenCV-python代码)
  20. 在EPS开发中遵循ISO 26262标准

热门文章

  1. 车牌识别程序python_车牌检测和识别的Python应用软件实现
  2. vertica 数据库 linux,配置访问列式数据库vertica的php环境
  3. plsql表锁被占用_处理Oracle被锁住的进程
  4. dos 初始化 mysql数据库_DOS下的MySQL数据库基本操作
  5. Softmax回归模型的构建和实现(Fashion-MNIST图像分类)
  6. Collection源码分析(二):LinkedList源码分析
  7. Adobe Photoshop CC 打开时报错~配置错误:请卸载并重新安装该产品
  8. 电脑密码忘记怎么办??
  9. Varnish——CDN推送平台(web页面批量清除缓存)
  10. 1-判断数字所在区间