原题链接在这里: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相关推荐

  1. 【同113】LeetCode 129. Sum Root to Leaf Numbers

    LeetCode 129. Sum Root to Leaf Numbers Solution1:我的答案 二叉树路径和问题,类似113 /*** Definition for a binary tr ...

  2. LeetCode Path Sum III(前缀和)

    问题: 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). 二叉树 ...

  3. LeetCode Combination Sum IV(动态规划)

    问题:给出一个数组nums和目标数target,问有多少组合形式 思路:用dp(i)表示目标数target的组合数.则有状态转移关系为dp(i)=sum(dp(i-nums[j])),其中i>= ...

  4. LeetCode Range Sum Query - Mutable(树状数组、线段树)

    问题:给出一个整数数组,求出数组从索引i到j范围内元素的总和.update(i,val)将下标i的数值更新为val 思路:第一种方式是直接根据定义,计算总和时直接计算从i到j的和 第二种方式是使用树状 ...

  5. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

  6. LeetCode Subarray Sum Equals K

    原题链接在这里:https://leetcode.com/problems/subarray-sum-equals-k/description/ 题目: Given an array of integ ...

  7. leetcode 907. Sum of Subarray Minimums | 907. 子数组的最小值之和(单调栈)

    题目 https://leetcode.com/problems/sum-of-subarray-minimums/ 题解 单调栈问题.参考左神算法课:https://ke.qq.com/webcou ...

  8. leetcode 371. Sum of Two Integers | 371. 两整数之和(补码运算)

    题目 https://leetcode.com/problems/sum-of-two-integers/ 题解 根据 related topics 可知,本题考察二进制运算. 第一次提交的时候,没想 ...

  9. [Leetcode]Two sum(两数之和)系列总结

    Two sum 题目 Given an array of integers, return indices of the two numbers such that they add up to a ...

  10. 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 = ...

最新文章

  1. apk,task,进程区别
  2. python json模块详解_深入解析Python编程中JSON模块的使用
  3. android使用sharepreference保存用户操作数据
  4. Spark 性能相关参数配置详解-Storage篇
  5. SharePoint 取消分享时的默认发邮件
  6. Java 爬虫简单起步
  7. 获取笔记本序列号(SN码)命令
  8. RH850中断使用方法
  9. golang json 转 map 切片 结构体
  10. 项目管理之项目风险应对
  11. Macbooster8免费mac清理垃圾软件功能介绍
  12. 网络和http协议理论
  13. android 有道笔记 装在哪个目录,有道云笔记文件保存在哪里在哪个路径下
  14. 2022.12六级真题第1套答案及详解(15页)
  15. 人人车否认破产称地方经营型模式升级为合伙人模式
  16. 25匹马赛跑确定前五匹马的问题
  17. “最粉嫩”的JVM垃圾回收器及算法,隔壁都馋哭了
  18. CSP考试复习:第一单元 C++语言基础1.6 其他常用操作!
  19. Android仿头条频道管理
  20. ubuntu(四)Ubuntu18.04安装微信

热门文章

  1. Android开发中的SQLite事务处理,即beginTransaction()方法
  2. VMware vSphere 5.1 群集深入解析(三)
  3. 专家答疑:揭开虚拟化的迷雾
  4. JWT(Json Web Token)介绍
  5. Linux 线程绑核(以后研究)
  6. Linux网络编程——多播
  7. docker容器打包成镜像
  8. springBoot过滤器去除请求参数前后空格
  9. 【渝粤教育】国家开放大学2018年春季 8668-21T汽车涂装技术(A) 参考试题
  10. 【sklearn第二十七讲】模型持久性