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. vant中tab标签切换时会改变内容滚动高度

    vant的tabs标签页,标签切换时会改变内容区的滚动高度,这是因为内容区共用同一个父元素为滚动区域引起的,解决办法:在tabs的内容区域嵌套一层滚动区域,让每个内容区域使用单独的滚动元素就行了.   ...

  2. 手把手建立Roofline模型(CPU)

    Roofline模型原理 Roofline模型是由加州理工大学伯利克提出的用来建立当前计算平台在不同的计算强度(Operational Intensity)下能够达到的理论计算上限 .论文和基础理论和 ...

  3. mavan的安装与配置

    1.下载mavan 下载路径:http://maven.apache.org/download.cgi 2.安装mavan 将下载好的压缩包解压到指定位置 3.配置系统环境变量 添加一个MAVAN_H ...

  4. Azure Container App(一)应用介绍

    一,引言 容器技术正日益成为打包.部署应用程序的第一选择.Azure 提供了许多使用容器的选项.例如,我们可以使用 Azure 容器注册表来存储和管理 Docker Images.Azure Cont ...

  5. Ansible 使用配置

    1.配置 /etc/ansible/hosts 文件,添加被管控主机ip #vim /etc/ansible/hosts   文件末尾添加组[group1]和被管控主机的IP [group1] 192 ...

  6. [SPDK/NVMe存储技术分析]013 - libibverbs API应用案例分析

    本文是对论文Dissecting a Small InfiniBand Application Using the Verbs API所做的中英文对照翻译 Dissecting a Small Inf ...

  7. 如何在 Java 中实现二叉搜索树

    二叉搜索树 二叉搜索树结合了无序链表插入便捷和有序数组二分查找快速的特点,较为高效地实现了有序符号表.下图显示了二叉搜索树的结构特点(图片来自<算法第四版>): 可以看到每个父节点下都可以 ...

  8. MySQL—事务(ACID)

    参考CSDN:https://blog.csdn.net/dengjili/article/details/82468576 1.事务四大特性 原子性(Atomicity) 要么都成功,要么都失败. ...

  9. path()和re_path()用法&区别

    path() 参数列表: 参数1:字符串类型,用来匹配请求路径 参数2:指定路径所对应的视图函数名 参数3:关键字参数 实际用的不多 参数4... # urls.py # 创建子应用的路由文件 fro ...

  10. gofs使用教程-基于golang的开源跨平台文件同步工具

    概述 gofs是基于golang开发的一款开箱即用的跨平台文件同步工具,开源地址如下:https://github.com/no-src/gofs,欢迎点个Star或者提交Issue和PR,共同进步! ...