LeetCode_167原题链接:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/

LeetCode_1099原题链接:https://leetcode-cn.com/problems/two-sum-less-than-k/

package Leetcode;
import java.util.Arrays;
import java.util.Scanner; /**
* @date 2022/4/3-18:47
* 给你一个下标从1开始的整数数组numbers,该数组已按非递减顺序排列,
* 请你从数组中找出满足相加之和等于目标数 target 的两个数。
*/
public class TwoSum_167 { // 使用双指针,判断 两端和 与 target 的大小关系
public static int[] twoSum(int[] numbers, int target) {
int n = numbers.length;
int left = 0 ;
int right = n - 1;
while (left < right) {
int sum = numbers[left] + numbers[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
return new int[] {left + 1, right + 1};
}
}
throw new IllegalArgumentException("No Solution");
} public static void main(String[] args) {
// int[] numbers = {2, 7, 11, 15};
// int target = 9;
System.out.println("Please input array separated by space or commas");
Scanner in = new Scanner(System.in);
String str = in.next().toString();
String[] strArr = str.split(",");
int[] numbers = new int[strArr.length];
for (int i = 0; i < strArr.length; i++) {
numbers[i] = Integer.parseInt(strArr[i]);
}
System.out.println(Arrays.toString(numbers));
System.out.println("Please input the value of target");
int target = in.nextInt();
in.close();
System.out.println(Arrays.toString(twoSum(numbers, target)));
}
}
 
leetCode_1099:给定的是无序数组A和一整数K,返回的是数组中的两个数的和尽可能接近整数K,但是不能取等号。
思路:跟上题一样,也是用双指针最方便,然后判断其和 与 整数K的大小关系。
注意:双指针使用前,必须有序,以及结果不存在时如何处理(定义初值)。
public int twoSumLessThanK(int[] A, int K) {
if (A == null || A.length <= 1) {
return -1;
}
Arrays.sort(A);
int left = 0;
int right = A.length - 1;
int res = Integer.MIN_VALUE;
while (left < right) {
int sum = A[left] + A[right];
if (sum < K) {
res = Math.max(res, sum);
left++;
} else {
right--;
}
}
return res == Integer.MIN_VALUE ? -1 : res;
}

两数之和II_LeetCode_167_1099的更多相关文章

  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. 【死磕NIO】— 跨进程文件锁:FileLock

    大家好,我是大明哥,一个专注于[死磕 Java]系列创作的程序员. [死磕 Java ]系列为作者「chenssy」 倾情打造的 Java 系列文章,深入分析 Java 相关技术核心原理及源码 死磕 ...

  2. ASP.NET Core 6.0对热重载的支持

    .NET 热重载技术支持将代码更改(包括对样式表的更改)实时应用到正在运行的程序中,不需要重启应用,也不会丢失应用状态. 一.整体介绍 目前 ASP.NET Core 6.0 项目都支持热重载.在以下 ...

  3. 开源版-阿里云人脸搜索M:N,人脸比对1:1

    一.人脸搜索概要 本项目是阿里云视觉智能开放平台的人脸1:N的开源替代,项目中使用的模型均为开源模型,项目支持milvus和proxima向量存储库,并具有较高的自定义能力. 项目使用纯Java开发, ...

  4. 记mysql5.7错误only_full_group_by

    错误截图为上 mysql版本:5.7.35-0ubuntu0.18.04.1 "this is incompatible with sql_mode=only_full_group_by&q ...

  5. django处理跨域

    django处理Ajax跨域访问时使用javascript进行ajax访问的时候,出现如下错误 出错原因:javascript处于安全考虑,不允许跨域访问.下图是对跨域访问的解释: 概念: 这里说的j ...

  6. 一文了解MySQL的Buffer Pool

    摘要:Innodb 存储引擎设计了一个缓冲池(Buffer Pool),来提高数据库的读写性能. 本文分享自华为云社区<MySQL 的 Buffer Pool,终于被我搞懂了>,作者:小林 ...

  7. 用腾讯云Gogs搭建私有git服务器

    前言 经常有需要写不能公开代码的项目,所以只好自己搭建一个私人的git服务器 Gogs的好处在于比Gitlib轻量化了好多,而且是国人写的,官方主页也是中文的 Gogs首页 腾讯云服务器配置: Ubu ...

  8. 用C语言读写数据

    //1-5题 #include "stdio.h" typedef struct { char name[10];//姓名 int subject1,subject2,subjec ...

  9. CentOS7安装redis5

    1.下载/解压redisredis手册地址:http://redisdoc.com/下载路径:https://redis.io/downloadtar zxvf redis包名 2.编译&安装 ...

  10. SpringBoot bean映射yml中的属性举例

    pom:导入配置文件处理器,配置文件进行绑定就会有提示 <dependency> <groupId>org.springframework.boot</groupId&g ...