两数之和_LeetCode_1
LeetCode_1原题链接:https://leetcode-cn.com/problems/two-sum/
剑指 Offer 57原题链接: https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/
package Leetcode;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner; /**
* @date 2022/4/3-17:26
* 给定一个整数数组 nums 和一个整数目标值 target,在该数组中找出和为目标值 target的那两个整数,
* 并返回它们的数组下标。
*/
public class TwoSum_1 { // 解法一:双重循环,判断相加的结果
public static int[] twoSum_1(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if(nums[i] + nums[j] == target){
return new int[]{i, j};
}
}
}
throw new IllegalArgumentException("No solution");
} // 解法二:定义Map集合,每次判断差值是否存在map中
public static int[] twoSum_2(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
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("No solution");
} public static void main(String[] args) {
// int[] nums = {1, 2, 3, 4};
// int target = 6;
System.out.println("Please input array separated by spaces or commas");
Scanner in = new Scanner(System.in);
String str = in.next().toString();
String[] strArr = str.split(",");
// String str = in.nextLine().toString();
// String[] strArr = str.split(" ");
int[] nums = new int[strArr.length];
for (int i = 0; i < nums.length; i ++) {
nums[i] = Integer.parseInt(strArr[i]);
}
System.out.println(Arrays.toString(nums));
System.out.println("Please input the value of target");
int target = in.nextInt();
in.close();
System.out.println(Arrays.toString(twoSum_1(nums, target)));
System.out.println(Arrays.toString(twoSum_2(nums, target)));
}
}
两数之和_LeetCode_1的更多相关文章
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- 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 ...
- LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
- 南大算法设计与分析课程OJ答案代码(1)中位数附近2k+1个数、任意两数之和是否等于给定数
问题1 用来测试的,就不说了 问题2:中位数附近2k+1个数 给出一串整型数 a1,a2,...,an 以及一个较小的常数 k,找出这串数的中位数 m 和最接近 m 的小于等于 m 的 k 个数,以及 ...
- 两数之和,两数相加(leetcode)
我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...
随机推荐
- MySQL CREATE TABLE 简单设计模板交流
推荐用 MySQL 8.0 (2018/4/19 发布, 开发者说同比 5.7 快 2 倍) 或同类型以上版本. CREATE TABLE TEMPLATE CREATE TABLE [table ...
- sql注入mysql注入
#跨库查询及应用思路 information_schema表特性,记录数据库名.表名.列名对应表 information_schema.schemata:存储所有数据库名 schema_name:数据 ...
- Delaunay三角剖分及MATLAB实例
https://blog.csdn.net/piaoxuezhong/article/details/68065170 一.原理部分 点集的三角剖分(Triangulation),对数值分析(如有限元 ...
- CF1481X Codeforces Round #699
C Fence Painting(构造) 有用的刷子贪心刷,没用的刷子填在后续的有用/已存在的位置(用个栈记一下就行) D AB Graph(图上构造) 把边当做三种类型,aa bb ab m为奇数时 ...
- 如何移植sqlite3到嵌入式平台
本人也时常看其他人的blog,搜索资料的目的是尽快解决当前的问题,例如如何移植一个ssh,尽快要明白所需的东西,方能移植完成. 移植sqlite3的步骤如下: 1. 到官方网站下载指定的源码包:htt ...
- MM32F0140的复位脚nRST复用成普通GPIO PA10功能
目录: 1.MM32F0020简介 2.MM32F0020的复位脚nRST和PA10的说明 3.MM32F0020的选项字节说明 4.MM32F0020的FLASH_OBR选项字节寄存器说明 5.MM ...
- 记-beego项目调用Jenkins API获取job信息
type JenkinsController struct { beego.Controller } type Job struct { Name string `json:"name&qu ...
- Java8 HashMap扩容时为什么不需要重新hash
技巧: 与&操作 和 与 n 如8 与,为0 则位置不变 https://blog.csdn.net/zlp1992/article/details/104376309 java8在 ...
- Kafka学习(一)
作者:普适极客链接:https://www.zhihu.com/question/53331259/answer/1321992772来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- 如何建立一个JDBC程序?
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...