LeetCode Two Sum
原题链接在这里:https://leetcode.com/problems/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.
Example:
Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
题解:
采用HashMap记录之前出现的num 和 其对应的 index.
Time Complexity: O(n). Space: O(n).
AC Java:
1 public class Solution { 2 public int[] twoSum(int[] nums, int target) { 3 if(nums == null || nums.length < 2){ 4 throw new IllegalArgumentException("Invalid input."); 5 } 6 7 int [] res = {-1, -1}; 8 HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); 9 for(int i = 0; i<nums.length; i++){ 10 if(!hm.containsKey(target - nums[i])){ 11 hm.put(nums[i], i); 12 }else{ 13 res[0] = hm.get(target-nums[i]); 14 res[1] = i; 15 break; 16 } 17 } 18 19 return res; 20 } 21 }
后面跟上Two Sum II - Input array is sorted, Two Sum III - Data structure design, Two Sum IV - Input is a BST, 3Sum, 3Sum Closest, 4Sum.
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/5040710.html
LeetCode Two Sum相关推荐
- 【同113】LeetCode 129. Sum Root to Leaf Numbers
LeetCode 129. Sum Root to Leaf Numbers Solution1:我的答案 二叉树路径和问题,类似113 /*** Definition for a binary tr ...
- LeetCode Path Sum III(前缀和)
问题: 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). 二叉树 ...
- LeetCode Combination Sum IV(动态规划)
问题:给出一个数组nums和目标数target,问有多少组合形式 思路:用dp(i)表示目标数target的组合数.则有状态转移关系为dp(i)=sum(dp(i-nums[j])),其中i>= ...
- LeetCode Range Sum Query - Mutable(树状数组、线段树)
问题:给出一个整数数组,求出数组从索引i到j范围内元素的总和.update(i,val)将下标i的数值更新为val 思路:第一种方式是直接根据定义,计算总和时直接计算从i到j的和 第二种方式是使用树状 ...
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
- LeetCode Subarray Sum Equals K
原题链接在这里:https://leetcode.com/problems/subarray-sum-equals-k/description/ 题目: Given an array of integ ...
- leetcode 907. Sum of Subarray Minimums | 907. 子数组的最小值之和(单调栈)
题目 https://leetcode.com/problems/sum-of-subarray-minimums/ 题解 单调栈问题.参考左神算法课:https://ke.qq.com/webcou ...
- leetcode 371. Sum of Two Integers | 371. 两整数之和(补码运算)
题目 https://leetcode.com/problems/sum-of-two-integers/ 题解 根据 related topics 可知,本题考察二进制运算. 第一次提交的时候,没想 ...
- [Leetcode]Two sum(两数之和)系列总结
Two sum 题目 Given an array of integers, return indices of the two numbers such that they add up to a ...
- leetcode -- 3 sum
3-sum 题目描写叙述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = ...
最新文章
- apk,task,进程区别
- python json模块详解_深入解析Python编程中JSON模块的使用
- android使用sharepreference保存用户操作数据
- Spark 性能相关参数配置详解-Storage篇
- SharePoint 取消分享时的默认发邮件
- Java 爬虫简单起步
- 获取笔记本序列号(SN码)命令
- RH850中断使用方法
- golang json 转 map 切片 结构体
- 项目管理之项目风险应对
- Macbooster8免费mac清理垃圾软件功能介绍
- 网络和http协议理论
- android 有道笔记 装在哪个目录,有道云笔记文件保存在哪里在哪个路径下
- 2022.12六级真题第1套答案及详解(15页)
- 人人车否认破产称地方经营型模式升级为合伙人模式
- 25匹马赛跑确定前五匹马的问题
- “最粉嫩”的JVM垃圾回收器及算法,隔壁都馋哭了
- CSP考试复习:第一单元 C++语言基础1.6 其他常用操作!
- Android仿头条频道管理
- ubuntu(四)Ubuntu18.04安装微信