当前位置:网站首页>LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
2022-06-10 20:00:00 【Li_XiaoJin】
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。
说明:
返回的下标值(index1 和 index2)不是从零开始的。
你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。
示例:
输入: numbers = [2, 7, 11, 15], target = 9
输出: [1,2]
解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
这个题两种解法都比较常见,要学会举一反三。
- 1、双指针法 定义首尾两个指针,将两个的值相加,如果得到的数大于目标数,则尾指针减一;如果得到的数小于目标数,则头指针加1。如果得到的数等于目标数则直接返回两个下标加1。
- 2、二分查找法 二分查找则是先设定一个数,再对后面的数进行二分查找,查看是否符合要求。
官方题解:在数组中找到两个数,使得它们的和等于目标值,可以首先固定第一个数,然后寻找第二个数,第二个数等于目标值减去第一个数的差。利用数组的有序性质,可以通过二分查找的方法寻找第二个数。为了避免重复寻找,在寻找第二个数时,只在第一个数的右侧寻找。
/**
* 167. 两数之和 II - 输入有序数组
* @Author: lixj
* @Date: 2020/10/9 14:25
*/
public class TwoSumIIInputarrayissorted {
/**
* 双指针法
* @param numbers
* @param target
* @return
*/
public int[] twoSum(int[] numbers, int target) {
int i = 0;
int j = numbers.length-1;
while (i < j) {
int sum = numbers[i] + numbers[j];
if (sum > target) {
j--;
} else if (sum < target) {
i++;
} else {
return new int[]{i+1, j+1};
}
}
return new int[]{-1, -1};
}
/**
* 二分查找法
* @param numbers
* @param target
* @return
*/
public int[] twoSum1(int[] numbers, int target) {
for (int i = 0; i < numbers.length; ++i) {
int low = i + 1;
int high = numbers.length -1;
while (low <= high) {
int mid = (high - low) /2 + low;
if (numbers[i] + numbers[mid] < target) {
low = mid + 1;
} else if (numbers[i] + numbers[mid] > target) {
high = mid - 1;
} else {
return new int[]{i+1, mid+1};
}
}
}
return new int[]{-1, -1};
}
public static void main(String[] args) {
int[] nums = new int[]{2, 7, 11, 13, 14};
TwoSumIIInputarrayissorted test = new TwoSumIIInputarrayissorted();
int[] a = test.twoSum1(nums, 10);
for (int i = 0; i < 2; i++) {
System.out.print(a[i] + " ");
}
}
}
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/167两数之和ii-输入有序数组
边栏推荐
- 六级考试-商务英语-考前最后一背
- 连接mysql报错 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
- vulnhub-The Planets: Earth
- Nanny tutorial: how to become a contributor to Apache linkis documents
- 72. 编辑距离 ●●●
- [technical fragment] implementation of renaming and filtering duplicate name files with suffixes
- Deploying static websites using OSS and CDN on Alibaba cloud international
- Read the source code of micropyton - add the C extension class module (1)
- 35岁被裁员,还能拥有美妙人生吗?
- Knife4j configuration can use direct copy
猜你喜欢

六级考试-商务英语-考前最后一背

Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?

Explain L3 cache to solve circular dependency

获取列表中最大最小值的前n个数值的位置索引的四种方法

电子招标采购商城系统:优化传统采购业务,提速企业数字化升级

Four methods to obtain the position index of the first n values of the maximum and minimum values in the list

Test APK exception control netlocation attacker development

2台电脑共享一套键盘鼠标

CVPR 2022 Tsinghua University proposed unsupervised domain generalization (UDG)

Microsoft Word 教程,如何在 Word 中更改页面方向、为页面添加边框?
随机推荐
View play and earn will lead crypto games astray
Four methods to obtain the position index of the first n values of the maximum and minimum values in the list
魔塔类游戏实现源码及关卡生成
pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)
Software definition boundary (SDP)
pytorch深度学习——卷积操作以及代码示例
mysql基础篇
面试必备——synchronized底层原理的基础知识
Nodejs: official document 3 Dgram stream
Attack and defense drill | network security "whistleblower": security monitoring
LeetCode:497. Random points in non overlapping rectangles -- medium
分布式服务理论基础
Microsoft Word 教程,如何在 Word 中更改页面方向、为页面添加边框?
堆排序与加强堆代码 用于记忆
User defined date component. The left and right buttons control forward or backward year, month, week and day turning
Redis集群配置
Knife4j configuration can use direct copy
The excess part of the table setting is hidden. Move the mouse to display all
Microsoft Word tutorial, how to change page orientation and add borders to pages in word?
Read the source code of micropyton - add the C extension class module (3)