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. 35个高级python知识点

    No.1 一切皆对象 众所周知,Java中强调"一切皆对象",但是Python中的面向对象比Java更加彻底,因为Python中的类(class)也是对象,函数(function) ...

  2. MATLAB绘制三角网及三维网线

    今天博主给大家介绍一些比较常见的可视化操作,绘制三角网及三维网线. 三角网是由一系列连续三角形构成的网状的平面控制图形,是三角测量中布设连续三角形的两种主要扩展形式,同时向各方向扩展而构成网状,优点为 ...

  3. 机器学习实战 | SKLearn最全应用指南

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/41 本文地址:http://www.showmeai.tech/article-det ...

  4. 使用阿里巴巴开源镜像站镜像——Kubernetes 镜像

    镜像下载.域名解析.时间同步请点击 阿里云开源镜像站 Kubernetes 镜像简介 Kubernetes 是一个开源系统,用于容器化应用的自动部署.扩缩和管理.它将构成应用的容器按逻辑单位进行分组以 ...

  5. redis哨兵功能

    redis哨兵功能 redis-Sentinel(哨兵) 前言 当用redis作master-slave的高可用时,如果master本身宕机,redis本身或者客户都没有实现主从切换的功能 redis ...

  6. eclipse新建maven项目:'Building' has encountered a problem. Errors occurred during the build.

    二.eclipse 新建maven 项目报错(因为没有配置maven环境) 1.问题: ① 出现的问题1: Could not calculate build plan:Plugin org.apac ...

  7. 内网渗透----域环境搭建(server 2008)

    域控制器 配置静态IP 安装域服务 点击服务器管理器-添加角色-下一步-添加AD域服务: 安装过后运行安装向导: 下一步后选择"在新林中新建域": 若提示密码不符合要求,则配置密码 ...

  8. 利用MSSQL getshell

    此次复现使用的sql server 2000 和sql server 2008两个环境进行的 是在已知数据库密码的基础上进行的 0x01 MSSQL连接 连接MSSQL 2000 新建连接: 填写目的 ...

  9. linux的文件与目录的权限设置

    @font-face { font-family: 宋体 } @font-face { font-family: "Cambria Math" } @font-face { fon ...

  10. python3 爬虫3--异常处理

    本文学习内容来自:https://germey.gitbooks.io/python3webspider/content/ urllib库中有URLError类,request模块产生的错误都可以通过 ...