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的更多相关文章

  1. 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

    题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...

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

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

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

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

  5. [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 ...

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

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

  7. Leetcode(一)两数之和

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

  8. 南大算法设计与分析课程OJ答案代码(1)中位数附近2k+1个数、任意两数之和是否等于给定数

    问题1 用来测试的,就不说了 问题2:中位数附近2k+1个数 给出一串整型数 a1,a2,...,an 以及一个较小的常数 k,找出这串数的中位数 m 和最接近 m 的小于等于 m 的 k 个数,以及 ...

  9. 两数之和,两数相加(leetcode)

    我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...

随机推荐

  1. PEP小学五年级英语下册 mp3 音频和电子书

    链接:https://pan.baidu.com/s/1O805uHU-lsMKog3WLtjRkA 提取码:o8rg 链接:https://pan.baidu.com/s/1Oa4wcM5min83 ...

  2. Mysql下载路径和安装

    下载路径 https://dev.mysql.com/downloads/mysql/ C:\Windows\system32>net start mysql 发生系统错误 2. 系统找不到指定 ...

  3. Net中事件的用法之一

    1.事件与委托的关系 委托是一种类型 事件是委托的一个实例 事件中涉及两种角色--事件发布者和事件订阅者. 事件发布者: 触发事件的对象称为事件发布者. 事件订阅者: 捕获事件并对其做出处理的对象称为 ...

  4. ArcGIS Server创建站点失败Failed to register the activation group

    ArcGIS Server:10.6 解决方法:右键我的电脑-->管理-->服务,重启arcgis server 服务

  5. 6月5日 python复习 模块

    """1. os和sys都是干什么的?2. 你工作中都用过哪些内置模块?3. 有没有用过functools模块?"""1. os 系统相关 ...

  6. Charles的安装及设置

    1. Charles的安装 l  下载安装包 l  安装:按照提示信息一步步安装,不做多余解释 l  破解:将Charles.jar包放在安装目录,如D:\Program Files\Charles\ ...

  7. python3 爬虫5--分析Robots协议

    1Robots协议 Robots协议告诉了搜索引擎和爬虫那些页面可以抓取,那些不可以,通常是存放在robots.txt文件里面,位于网站的根目录下 robots.txt中内容的示范: User-age ...

  8. TreeMap与TreeSet的源码分析

    1.TreeMap源码 1.属性部分: private final Comparator<? super K> comparator;//比较器 private transient Ent ...

  9. RabbitMQ-learning

    第一种模式=直连 P:生产者,也就是要发送消息的程序 C:消费者:消息的接受者,会一直等待消息到来. queue:消息队列,图中红色部分.类似一个邮箱,可以缓存消息:生产者向其中投递消息,消费者从其中 ...

  10. 详解AOP——用配置文件的方式实现AOP

    AOP概念 1.AOP:面向切面(方面)编程,扩展功能不修改源代码实现 AOP原理 AOP采用横向抽取机制,取代了传统纵向继承体系重复性代码 传统的纵向抽取机制: 横向抽取机制: AOP操作术语 1. ...