LeetCode-1.Two Sum
1.题目描述
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].
2.我的分析思路
首先,最直接的思路就是遍历数组,分两次遍历,找到结果后直接返回即可。直接上代码:
public static int[] twoSum(int[] nums, int target) throws IllegalArgumentException {for (int i = 0; i < nums.length; i++) {for (int j = i + 1; j < nums.length; j++) {if (nums[j] == target - nums[i]) {return new int[] { i, j };}}}throw new IllegalArgumentException("no num found");
}
这个算法的时间复杂度为
O(n^2)
空间复杂度为:
O(1)
3.其他的思路
3.1 思路1
将所有数组内的下标和值存储到一个map中,然后只需要遍历一次数组,每个数据进行计算,算出对应的差值,如果这个差值在map中存在,那么就直接返回两个下标,否则抛出异常。
public static int[] twoSum(int[] nums, int target) {Map<Integer, Integer> map = new HashMap<>();for (int i = 0; i < nums.length; i++) {map.put(nums[i], i);}for (int i = 0; i < nums.length; i++) {int complement = target - nums[i];if (map.containsKey(complement) && map.get(complement) != i) {return new int[] { i, map.get(complement) };}}throw new IllegalArgumentException("No two sum solution");
}
分析这个算法,可以得到,这个算法的时间复杂度为:
O(n)
空间复杂度为:
O(n)
是典型的空间换时间的算法。
思路2
还是空间换时间的思路,直接遍历数组,计算差值,如果在map中存在这个值,直接返回,否则将数组中的值存入map中。
public static int[] twoSum(int[] nums, int target) {Map<Integer, Integer> map = new HashMap<>();for (int i = 0; i < nums.length; i++) {int complement = target - nums[i];if (map.containsKey(complement)) {return new int[] { map.get(complement), i };}map.put(nums[i], i);}throw new IllegalArgumentException("No two sum solution");
}
分析这个算法,可以得到,这个算法的时间复杂度为:
O(n)
空间复杂度为:
O(n)
是典型的空间换时间的算法。
转载于:https://www.cnblogs.com/f-zhao/p/6369765.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 ...
最新文章
- php怎么解析xml,使用PHP快速解析复杂的XML文件
- 保存文件_wps文件保存在哪里
- java this 方法,使用“this”用方法(用Java)
- 云起智慧中心连接华为_云起荣获CIBIS十大全屋智能品牌奖:将与合作伙伴共同扩展AIoT生态平台...
- win7此更新不适用计算机,win7系统更新提示“此更新不适用于您的计算机”的解决方法...
- 2500个常用中文字符 + 130常用中英文字符
- 学会 Python 到底能干嘛?我们整理出了 7 大工作方向……
- 【jdk1.8特性】之Function
- 《梁宁产品思维30讲》
- 农业农村部:发现生猪、野猪异常死亡应立即报告
- linux编译n2n v2,在centos下安装n2n服务器
- 计算机死机重装xp,XP系统电脑死机如何急救
- 微信公众平台开发之微信红包的实现
- java Ofd 转图片_一文教你搞懂如何将ofd格式的文件转为图片
- 你的孤独,正在撑起一个万亿级市场
- MYSQL操作数据库-------查看、删除数据库
- Ping和traceroute的原理
- 人际关系-《你为什么不道歉》书中的精髓:道歉对于人际关系的重要性,以及怎样传达有诚意、有深度的道歉。
- 分布式 PostgreSQL 集群(Citus),官方快速入门教程
- 学习日志 -- Day02
热门文章
- java.util.concurrent.locks.Condition 例子程序探讨
- LDAP之LDAP服务器搭建
- A*算法 javascript模拟
- asp.net 创建文件夹时出错:未找到路径“D:\”的一部分。(asp.net 使用 FSO)
- Spring总结之SpringMvc下
- 79. Word Search
- 一次下载与导出的博弈
- 学习angularjs时遇到 XX is not a function
- 完成端口模式下的高性能网络服务器【转】
- android Anr Input类型系统源码解析