题目描述

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9

所以返回 [0, 1]

思路

思路一:

暴力

思路二:

用 HashMap 存储数组元素和索引的映射,在访问到 nums[i] 时,判断 HashMap 中是否存在 target - nums[i] ,如果存在说明 target - nums[i] 所在的索引和 i 就是要找的两个数。该方法的时间复杂度为 O(N),空间复杂度为 O(N),使用空间来换取时间。

代码实现

  1. package Array;
  2. import java.util.Arrays;
  3. import java.util.HashMap;
  4. /**
  5. * 两数之和
  6. * 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
  7. * 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
  8. */
  9. public class Solution1 {
  10. public static void main(String[] args) {
  11. Solution1 solution1 = new Solution1();
  12. int[] nums = {2, 7, 11, 15};
  13. int target = 9;
  14. int[] result = solution1.twoSum(nums, target);
  15. System.out.println(Arrays.toString(result));
  16. }
  17. /**
  18. * 用 HashMap 存储数组元素和索引的映射,在访问到 nums[i] 时,判断 HashMap 中是否存在 target - nums[i]
  19. * 如果存在说明 target - nums[i] 所在的索引和 i 就是要找的两个数。
  20. * 该方法的时间复杂度为 O(N),空间复杂度为 O(N),使用空间来换取时间。
  21. *
  22. * @param nums
  23. * @param target
  24. * @return
  25. */
  26. public int[] twoSum_2(int[] nums, int target) {
  27. HashMap<Integer, Integer> map = new HashMap<>();
  28. for (int i = 0; i < nums.length; i++) {
  29. if (map.containsKey(target - nums[i])) {
  30. return new int[]{map.get(target - nums[i]), i};
  31. } else {
  32. map.put(nums[i], i);
  33. }
  34. }
  35. return null;
  36. }
  37. /**
  38. * 暴力,时间复杂度为 O(N^2)
  39. *
  40. * @param nums
  41. * @param target
  42. * @return
  43. */
  44. public int[] twoSum(int[] nums, int target) {
  45. for (int i = 0; i < nums.length; i++) {
  46. for (int j = i + 1; j < nums.length; j++) {
  47. if (nums[i] + nums[j] == target) {
  48. return new int[]{i, j};
  49. }
  50. }
  51. }
  52. return null;
  53. }
  54. }

Leetcode#1.Two Sum(两数之和)的更多相关文章

  1. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  2. [LeetCode]1.Two Sum 两数之和&&第一次刷题感想

    ---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...

  3. [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)

    题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...

  4. 【LeetCode】Two Sum(两数之和)

    这道题是LeetCode里的第1道题. 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会 ...

  5. [leetcode]1. Two Sum两数之和

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

  6. [LeetCode]1.Two Sum 两数之和(Java)

    原题地址:two-sum 题目描述: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标. 你可以假设每 ...

  7. LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

    1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...

  8. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  9. Leetcode:0002(两数之和)

    LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...

  10. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. 斯坦福大学公开课机器学习: advice for applying machine learning | regularization and bais/variance(机器学习中方差和偏差如何相互影响、以及和算法的正则化之间的相互关系)

    算法正则化可以有效地防止过拟合, 但正则化跟算法的偏差和方差又有什么关系呢?下面主要讨论一下方差和偏差两者之间是如何相互影响的.以及和算法的正则化之间的相互关系 假如我们要对高阶的多项式进行拟合,为了 ...

  2. C和 C++的特点

    C语言进化到C++ 的过程,是一个障眼法的发展过程.1. bool型变量:1个字节变量(和char一样大小),缺省赋值为true(1),false(0)2. 引用型变量:让新申请的变量挂在原有同类型的 ...

  3. RS485 / RS422

    RS422可以变为RS485:A和Y短路(然后接T/R+),B和Z短路(然后接T/R-) RS485是半双工,只有两根线通信线,要么接收状态,要么发送状态 RE为低电平,作为接收器 DE为高电平,作为 ...

  4. 插入排序Java版

    package dataStructureAlgorithmReview.day01; import java.util.Arrays; /** * 插入排序 * @author shundong * ...

  5. 关于vue的小实例

    学习网址:http://www.runoob.com/vue2/vue-tutorial.html 下面是我在上面学着写的两个小例子, 1. 实现点击全选,下面的均被选中,再点击一下,下面的均取消选择 ...

  6. Tensorflow object detection API 搭建物体识别模型(三)

    三.模型训练 1)错误一: 在桌面的目标检测文件夹中打开cmd,即在路径中输入cmd后按Enter键运行.在cmd中运行命令: python /your_path/models-master/rese ...

  7. 使用Nexus配置Maven私有仓库

    使用Nexus配置Maven私有仓库 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装配置Nexus 1>.下载nexus 下载地址:https://www.sonat ...

  8. Hadoop ha CDH5.15.1-hadoop集群启动后,两个namenode都是standby模式

    Hadoop ha CDH5.15.1-hadoop集群启动后,两个namenode都是standby模式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一说起周五,想必大家都特别 ...

  9. mac 安装pip

    mac 安装pip报错 bogon:~ root# sudo easy_install pip Searching for pip Reading https://pypi.python.org/si ...

  10. CSS常用选择器的认识

    ---恢复内容开始--- 前言:在CSS中选择器的种类有很多很多,但是在实际的工作中,我们经常会用到的分为两大类:基础选择器和复合选择器这两个大类,学习选择器的目的就是为了在复杂的页面中能够快速定位到 ...