当前位置:网站首页>167. 两数之和 II - 输入有序数组
167. 两数之和 II - 输入有序数组
2022-07-29 05:09:00 【卷饼85】
题目描述
给你一个下标从 1 开始的整数数组 numbers ,该数组已按 非递减顺序排列 ,请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1] 和 numbers[index2] ,则 1 <= index1 < index2 <= numbers.length 。
以长度为 2 的整数数组 [index1, index2] 的形式返回这两个整数的下标 index1 和 index2。
你可以假设每个输入 只对应唯一的答案 ,而且你 不可以 重复使用相同的元素。
你所设计的解决方案必须只使用常量级的额外空间。
示例 1:
输入:numbers = [2,7,11,15], target = 9
输出:[1,2]
解释:2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。返回 [1, 2] 。
示例 2:
输入:numbers = [2,3,4], target = 6
输出:[1,3]
解释:2 与 4 之和等于目标数 6 。因此 index1 = 1, index2 = 3 。返回 [1, 3] 。
示例 3:
输入:numbers = [-1,0], target = -1
输出:[1,2]
解释:-1 与 0 之和等于目标数 -1 。因此 index1 = 1, index2 = 2 。返回 [1, 2] 。
提示:
2 <= numbers.length <= 3 * 104
-1000 <= numbers[i] <= 1000
numbers 按 非递减顺序 排列
-1000 <= target <= 1000
仅存在一个有效答案
题解
方法一:双指针
最优
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
//双指针 时间复杂度O(n)
int i=0,j=numbers.size()-1;
while(j>i)
{
if(numbers[i]+numbers[j]>target){
j--;}
else if(numbers[i]+numbers[j]==target){
return {
i+1,j+1};}
else i++;
}
return {
-1,-1};
方法二:二分查找
//二分查找
for (int i = 0; i < numbers.size(); ++i) {
int low = i + 1, high = numbers.size() - 1,k = target - numbers[i];
while (low <= high)
{
int mid = low + (high - low)/2 ;
if (numbers[mid] == k)
{
return {
i + 1, mid + 1};
}
else if (numbers[mid] > k)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
}
return {
-1, -1};
}
//这个很蠢,用上面那个
int i = 0,k=0;
int left=0 , right=numbers.size(),mid=0;
while(left<=right)
{
left=i+1;
k = target-numbers[i];
mid = left+(right-left)/2;
if(numbers[mid]>k){
right = mid-1;}
else if(numbers[mid]==k)return {
i+1,mid+1};
else if(numbers[mid]<k)
{
for(mid = mid+1;mid<right;mid++)
{
if(numbers[mid]>k)break;
else if(numbers[mid]==k)return {
i+1,mid+1};
}
++i;
}
}
return {
0,0};
}
};
边栏推荐
- MySQL的基础概念+数据库系统结构+拓展延申+基础命令学习
- QML定制TabBar
- 平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道
- 什么是_GLIBCXX_VISIBILITY(default)
- MySQL的详细安装使用教程(保姆式安装图文讲解)
- ARFoundation入门教程10-平面检测和放置
- Database course design of online assistant teaching platform for high school chemistry
- NumPy基础
- 直播预告:京东云DevOps与JFrog制品库的融合
- This article takes you to understand the implementation of surround notification @around and final notification @after
猜你喜欢
平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道
法线可视化
How rimworld uploads creative workshops through steamcmd
CMU15-213 Malloc Lab实验记录
Qt版的贪食蛇游戏项目
Arfoundation starts from scratch 8-geospatial API (geospatial) development
Arfoundation starts from zero 9-ar anchor
ARFoundation入门教程7-url动态加载图像跟踪库
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
Live broadcast preview | how to save 30% labor cost and shorten 80% trademark processing cycle?
随机推荐
浅谈AspectJ框架
基于注解的三层项目的改造及添加包扫描的方式
C语言函数实现输出I love you
ARFoundation从零开始8-Geospatial API(地理空间)开发
三层项目的架构分析及构造方法的参数名称注入
为啥谷歌的内部工具不适合你?
QML type: state state
自定义Qml控件:ImageButton
京东云分布式链路追踪在金融场景的最佳实践
QT series - Installation
scikit-learn——机器学习应用开发的步骤和理解
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
7.1-default-arguments
WDDM learning
During the appointment, the 2022 JD cloud industrial integration new product launch was launched online
MySQL的基础概念+数据库系统结构+拓展延申+基础命令学习
Numpy Foundation
最新坦克大战2022-全程开发笔记-2
C语言连连看秒杀辅助
英伟达周锡健:设计到数字营销的最后一公里