LeetCode01 - 两数之和(Java 实现)
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. 两数之和 Java解法
这题属于Leetcode的签到题,基本上每个人一进来就是这题. 用哈希思想来做就是最好的解答. 如果一个target - num[i] 存在那么就返回那个数字对应的下标和当前元素的下标. public ...
- 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 ...
- leetCode:twoSum 两数之和 【JAVA实现】
LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...
- [Java]1.两数之和 - LeetCode
1 题目 2 思路与代码 思路一:暴力法(两层For循环) 时间复杂度:O(n^2) 对于每个元素,我们试图通过遍历数组的其余部分来寻找它所对应的目标元素,这将耗费 O(n) 的时间.因此时间复杂度为 ...
- Java实现 LeetCode 653 两数之和 IV - 输入 BST(递归,找差值)
653. 两数之和 IV - 输入 BST 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true. 案例 1: 输入: 5 / \ 3 6 / ...
- Java实现 LeetCode 1两数之和
1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...
- Java实现 LeetCode 167 两数之和 II - 输入有序数组
167. 两数之和 II - 输入有序数组 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- 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 ...
随机推荐
- Mac 安装Mysql 之 Sqlservice 区别
Mysql 一.下载mysql 官网“Community “ 下会看到“MySQL Community Server”下方有一个“download”点击. 在Mac OS上的MySQL的版本 ...
- cnpm 安装vue与vue/cli
cnpm 安装 npm install -g cnpm --registry=https://registry.npm.taobao.org 安装 vue npm install -g npm 安装 ...
- 《Java语言程序设计》第三讲类与对象“动手动脑”
一.以下代码为何无法通过编译?哪儿出错了? 答: 如果类提供了一个自定义的构造方法,将导致系统不再提供默认构造方法. 二. (1)以下代码输出结果是什么? package xy; public cla ...
- 【图像处理】H.264开源解码器评测
转自:http://wmnmtm.blog.163.com/blog/static/38245714201142883032575/ 要播放HDTV,就首先要正确地解开封装,然后进行视频音频解码.所以 ...
- 使用sequelize-auto生成sequelize的Models
一.全局安装sequelize-auto npm install -g sequelize-auto 二.全局安装对应数据库的驱动,此处使用的是mysql npm install -g mysql 三 ...
- ip地址查询python3小工具_V0.0.1
看到同事在一个一个IP地址的百度来确认导出表格中的ip地址所对应的现实世界的地址是否正确,决定给自己新开一个坑.做一个查询ip“地址”的python小工具,读取Excel表格,在表格中的后续列输出尽可 ...
- 【Python】【demo实验12】【练习实例】【列表的复制】
#!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- # 将一个列表复制到另外一个列表中: # 分析:可以使用[:] L = [0,3, ...
- Oracle-DQL 7- 集合操作
集合操作: --将查询结果看作是一个集合,可以将多个查询结果之间用集合操作找出特点的数据--很多的集合操作可以使用条件的组合进行代替,集合操作的效率高于条件组合--某些复杂的查询结果只能通过集合操作得 ...
- 「java.util.concurrent并发包」之 ThreadPoolExecutor
一 异步用new Thread? 大写的"low"!! new Thread(new Runnable() { @Override public void run() { // T ...
- GCD and LCM HDU - 4497(质因数分解)
Problem Description Given two positive integers G and L, could you tell me how many solutions of (x, ...