两数之和II_LeetCode_167_1099
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的更多相关文章
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- 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 ...
- 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 ...
- [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 ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
- 南大算法设计与分析课程OJ答案代码(1)中位数附近2k+1个数、任意两数之和是否等于给定数
问题1 用来测试的,就不说了 问题2:中位数附近2k+1个数 给出一串整型数 a1,a2,...,an 以及一个较小的常数 k,找出这串数的中位数 m 和最接近 m 的小于等于 m 的 k 个数,以及 ...
- 两数之和,两数相加(leetcode)
我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...
随机推荐
- Python通过snap7库与西门子S7-1200建立S7通信,读写存储器数据,顺便写个流水灯
1.snap7 简介 snap7 是一个基于以太网与S7系列的西门子PLC通讯的开源库. 支持包括S7系列的S7-200.S7-200 Smart.S7-300.S7-400.S7-1200以及S7- ...
- 22.1.22 并查集和KMP算法
22.1.22 并查集和KMP算法 1.并查集结构 1)实现: 并查集有多种实现方式,例如向上指的图的方式,数组的方式等等.其根本思想就在于准确记录某个节点的根节点,这个这种记录就能够很快的实现并查集 ...
- HTML-置换元素
我们都知道,行内元素不能够定义宽度和高度,但 img,input,button等标签作为行内元素却可以定义宽高,为什么呢?这就牵扯到了置换元素和非置换元素. 置换元素: w3c官方解释:"A ...
- React算法复杂度优化?
react树对比是按照层级去对比的, 他会给树编号0,1,2,3,4.... 然后相同的编号进行比较.所以复杂度是n,这个好理解. 关键是传统diff的复杂度是怎么算的?传统的diff需要出了上面的比 ...
- 什么是bean的自动装配?
Spring 容器能够自动装配相互合作的bean,这意味着容器不需要<constructor-arg>和<property>配置,能通过Bean工厂自动处理bean之间的协作.
- windows服务器下frp实现内网穿透
一.操作步骤 1.服务器:首先在服务器上解压到相应目录并配置frps.ini文件如下: 2.服务器:按下windows+R输入cmd进入命令窗口,进入到安装目录下运行frps.exe -c frps. ...
- @Component, @Controller, @Repository, @Service 有何区别?
@Component :这将 java 类标记为 bean.它是任何 Spring 管理组件的通 用构造型.spring 的组件扫描机制现在可以将其拾取并将其拉入应用程序环境 中. @Controll ...
- MySQL 里有 2000w 数据,redis 中只存 20w 的数据,如 何保证 redis 中的数据都是热点数据?
Redis 内存数据集大小上升到一定大小的时候,就会施行数据淘汰策略. 相关知识:Redis 提供 6 种数据淘汰策略: volatile-lru:从已设置过期时间的数据集(server.db[i]. ...
- Tomcat警告之“资源添加到Web应用程序[]的缓存中,因为在清除过期缓存条目后可用空间仍不足 - 请考虑增加缓存的最大空间”
原因 缓存不够,可以将其缓存调大 解决办法: 在 /conf/context.xml 的 前添加以下内容: <Resources cachingAllowed="true" ...
- 用css动态实现圆环百分比分配——初探css3动画
最近的小程序项目有个设计图要求做一个圆环,两种颜色分配,分别代表可用金额和冻结金额.要是就直接这么显示,感觉好像挺没水平??于是我决定做个动态! 在mdn把新特性gradients(渐变).trans ...