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. 从零开始,开发一个 Web Office 套件(11):支持中文输入法(or 其它使用输入法的语言)

    这是一个系列博客,最终目的是要做一个基于 HTML Canvas 的.类似于微软 Office 的 Web Office 套件(包括:文档.表格.幻灯片--等等). 博客园:<从零开始, 开发一 ...

  2. Ubuntu20.04安装RabbitMQ

    本博客旨在自我学习使用,如有任何疑问请及时联系博主 安装erlang 由于RabbitMq需要erlang语言的支持,在安装RabbitMq之前需要安装erlang sudo apt-get inst ...

  3. BUU [GKCTF 2021]签到

    BUU [GKCTF 2021]签到 1.题目概述 2.解题过程 追踪HTTP流 在下面发现了一串可疑字符 Base16转base64 放到010里看看 复制下来,去转字符 好像不是,再回去找找其他的 ...

  4. 4月19日 python学习总结 套接字模块的使用

    服务端: import socket phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 买电话 phone.bind(('127.0.0 ...

  5. 3D视觉 之 线激光3D相机

    1  3D 视觉 常见的三维视觉技术,包含双目.ToF.激光三角.结构光等,如下图:     1)毫米级 双目.ToF.结构光(散斑)的精度为 mm 级,多见于消费领域,如:导航避障,VR/AR,刷脸 ...

  6. java高级用法之:调用本地方法的利器JNA

    目录 简介 JNA初探 JNA加载native lib的流程 本地方法中的结构体参数 总结 简介 JAVA是可以调用本地方法的,官方提供的调用方式叫做JNI,全称叫做java native inter ...

  7. 半吊子菜鸟学Web开发3 --Html css学习1

    1创建一个html文件,用vscode打开 首先输入一个! 然后就可以开始编辑html文件了 2 整体结构 <!DOCTYPE HTML><html>    <head& ...

  8. maven-something

    <!--dependencyManagement提供一种管理依赖版本好的方式--> <!-- 通常出现在项目的最顶层父POM,--> <!-- 可以让所有在子项目中引用的 ...

  9. 说说 RPC 的实现原理?

    首先需要有处理网络连接通讯的模块,负责连接建立.管理和消息的传输.其次需要有编解码的模块,因为网络通讯都是传输的字节码,需要将我们使用的对象序列化和反序列化.剩下的就是客户端和服务器端的部分,服务器端 ...

  10. Spring 支持的事务管理类型?

    Spring 支持两种类型的事务管理:编程式事务管理:这意味你通过编程的方式管理事务,给你带来极大的灵 活性,但是难维护.声明式事务管理:这意味着你可以将业务代码和事务管理分离,你只需用 注解和 XM ...