LeetCode01 - 两数之和(Java 实现)

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/two-sum

题目描述

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

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

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

Java 实现与实现思路

import java.util.HashMap;

/**
* <p>
* 01:两数之和
* 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
* 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
* </p>
*
* @since 2019-07-14
* @author XiaoPengwei
*/
public class LeetCode01TwoSum { public static void main(String[] args) { int[] testArr = {2, 7, 10, 32, 21};
int target = 9; System.out.println("--> the method 1");
int[] ints1 = method1(testArr, target);
for (int i1 : ints1) {
System.out.println(i1);
} System.out.println("--> the method 2");
int[] ints2 = method2(testArr, target);
for (int i2 : ints2) {
System.out.println(i2);
} System.out.println("--> the method 3");
int[] ints3 = method3(testArr, target);
for (int i3 : ints3) {
System.out.println(i3);
}
} /**
* 方法一:暴力法
* 两层循环。外层先拿出左侧第 1 个元素,内层依次判断右侧其余元素与它的和如果等于目标值则返回,如果不等于则继续下一个,如果全部遍历完不存在则抛出异常。
*
* @param nums 数组
* @param target 和
* @return int[] 下标数组
*/
public static int[] method1(int[] nums, int target) { for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] + nums[i] == target) {
return new int[]{i, j};
}
} }
throw new IllegalArgumentException("Method1: No two sum solution");
} /**
* 方法二:用 Hash 表
* (1)构造一个哈希表,key 是所有待选数组元素,value 是数组下标;
* (2)然后从左向右遍历,对于元素 i,判断 target - nums[i] 是否包含在哈希表中,如果存在则拿出下标,如果不存在则继续。
*
* @param nums 数组
* @param target 和
* @return int[] 下标数组
*/
public static int[] method2(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int temp = target - nums[i];
if (map.containsKey(temp) && map.get(temp) != i) {
return new int[]{i, map.get(temp)};
}
} throw new IllegalArgumentException("Method2: No two sum solution");
} /**
* 方法三:用 Hash 表
* 可以不单独构造哈希表。这样显然在第一个元素时,不可能匹配。为什么这样也可以呢?
* 其实上面我们程序结束的条件是对 2,查找是否包含 7。而这里是对 7,查找包含 2。同样可以实现。
*
* @param nums 数组
* @param target 和
* @return int[] 下标数组
*/
public static int[] method3(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
int temp = target - nums[i];
if (map.containsKey(temp)) {
return new int[]{
map.get(temp), i};
}
map.put(nums[i], i);
} throw new IllegalArgumentException("Method3: No two sum solution");
}
}

LeetCode01 - 两数之和(Java 实现)的更多相关文章

  1. 1. 两数之和 Java解法

    这题属于Leetcode的签到题,基本上每个人一进来就是这题. 用哈希思想来做就是最好的解答. 如果一个target - num[i] 存在那么就返回那个数字对应的下标和当前元素的下标. public ...

  2. 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 ...

  3. leetCode:twoSum 两数之和 【JAVA实现】

    LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...

  4. [Java]1.两数之和 - LeetCode

    1 题目 2 思路与代码 思路一:暴力法(两层For循环) 时间复杂度:O(n^2) 对于每个元素,我们试图通过遍历数组的其余部分来寻找它所对应的目标元素,这将耗费 O(n) 的时间.因此时间复杂度为 ...

  5. Java实现 LeetCode 653 两数之和 IV - 输入 BST(递归,找差值)

    653. 两数之和 IV - 输入 BST 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true. 案例 1: 输入: 5 / \ 3 6 / ...

  6. Java实现 LeetCode 1两数之和

    1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...

  7. Java实现 LeetCode 167 两数之和 II - 输入有序数组

    167. 两数之和 II - 输入有序数组 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必 ...

  8. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  9. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

随机推荐

  1. Mac 安装Mysql 之 Sqlservice 区别

    Mysql 一.下载mysql    官网“Community “ 下会看到“MySQL Community Server”下方有一个“download”点击.   在Mac OS上的MySQL的版本 ...

  2. 【VS开发】C++ opencv Mat基础

    OpenCV2:Mat 1.Mat基础 在计算机内存中,数字图像是已矩阵的形式保存的.OpenCV2中,数据结构Mat是保存图像像素信息的矩阵,它主要包含两部分:矩阵头和一个指向像素数据的矩阵指针. ...

  3. NDK学习笔记-C语言

    本文简要回顾了C语言的一些注意事项和理解细节,不再赘述C语言的所有语法 头文件 头文件作为引入文件,在编译的时候,加载到源代码,参与编译 在VS2013中可以看到,当引入头文件时候,只能看到函数的声明 ...

  4. PHP学习(4)——数组的使用

    1.数组的概念 数组就是一个用来存储一系列变量值的命名区域. 每个数组元素有一个相关的索引(也成为关键字),它可以用来访问元素. PHP允许间隔性地使用数字或字符串作为数组的索引. 2.数字索引数组 ...

  5. [bzoj3357][Usaco2004]等差数列_动态规划_贪心

    [Usaco2004]等差数列 题目大意:约翰发现奶牛经常排成等差数列的号码.他看到五头牛排成这样的序号:“1,4,3,5,7”很容易看出“1,3,5,7”是等差数列.给出N(1≤N≤2000)数字A ...

  6. 【转帖】知乎管理华为鸿蒙OS的介绍2

    作者:虎游链接:https://www.zhihu.com/question/328382980/answer/784629132来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  7. PAT A1020 Tree Traversals(25)

    题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

  8. dij 费用流

    #include <bits/stdc++.h> using namespace std; typedef long long lld; const int MAXN = 50010, M ...

  9. python元组数据类型讲解

    元组可以被看成是不能改变的列表.列表是动态的,你可以增添,插入,删除,更改列表元素.有时我们需要这样的操作,但是有些时候我们需要保证有些数据是不能被用户或程序更改的.这就是元组的作用. 准确的说,列表 ...

  10. Dom编程-左侧菜单栏设计模型实现

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...