[leetcode]1.Two Sum
题目
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相关推荐
- leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)
题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...
- 【动态规划】LeetCode 377. Combination Sum IV
LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...
- 【DFS】LeetCode 39. Combination Sum
LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...
- LeetCode 167.Two Sum II 解题报告
LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...
- leetcode 1005 Maximize Sum Of Array After K Negations
leetcode 1005 Maximize Sum Of Array After K Negations 1.题目描述 2.解题思路 3.Python代码 1.题目描述 给定一个整数数组 A,我们只 ...
- [勇者闯LeetCode] 1. Two Sum
[勇者闯LeetCode] 1. Two Sum Description Given an array of integers, return indices of the two numbers s ...
- [勇者闯LeetCode] 112. Path Sum
[勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...
- 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...
- 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 ...
- 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 ...
最新文章
- 数据分析工具Pandas(4):Pandas的函数应用
- js 删除数组几种方法
- SCRIPT LOAD lua文件
- Jedis干什么用的
- 【学术相关】是什么让女性在计算机史上“隐身”了?
- shell查找命令大全
- base32解码工具_[随波逐流]CTF编码工具 V1.0
- 斯坦福大学博士后王鸿伟: 知识图谱辅助的个性化推荐系统
- 数学建模——sas(1)——几种统计方法
- 网站压力测试的几种方法
- Vue中如何解决跨域问题
- android tv 国内使用,不花钱解决 Android TV 原生系统国内APP不显示图标
- 必须了解的网络运维知识
- 生鲜配送ERP系统_对商品模块数据模型与界面设计的思考【Java 开源版】杭州生鲜配送系统_升鲜宝_SaaS全链路生鲜供应链管理系统_升鲜宝
- 婚介行业线上引流渠道哪些?你还在为找不到客户而烦恼吗?这些渠道千万别错过!
- DWZ的几个常用组件
- 冷笑话 企鹅与北极熊
- Linux实用的快捷键
- 机器视觉实验四: 为人脸添加装饰物特效实验(OpenCV-python代码)
- 在EPS开发中遵循ISO 26262标准
热门文章
- 车牌识别程序python_车牌检测和识别的Python应用软件实现
- vertica 数据库 linux,配置访问列式数据库vertica的php环境
- plsql表锁被占用_处理Oracle被锁住的进程
- dos 初始化 mysql数据库_DOS下的MySQL数据库基本操作
- Softmax回归模型的构建和实现(Fashion-MNIST图像分类)
- Collection源码分析(二):LinkedList源码分析
- Adobe Photoshop CC 打开时报错~配置错误:请卸载并重新安装该产品
- 电脑密码忘记怎么办??
- Varnish——CDN推送平台(web页面批量清除缓存)
- 1-判断数字所在区间