当前位置:网站首页>167. Sum of two numbers II - enter an ordered array
167. Sum of two numbers II - enter an ordered array
2022-07-29 05:27:00 【Tortilla 85】
167. Sum of two numbers II - Input an ordered array
Title Description
I'll give you a subscript from 1 The starting array of integers numbers , The array has been pressed Non decreasing order , Please find out from the array that the sum satisfying the addition is equal to the target number target Two numbers of . Let these two numbers be numbers[index1] and numbers[index2] , be 1 <= index1 < index2 <= numbers.length .
In length 2 Array of integers for [index1, index2] Returns the subscript of these two integers in the form of index1 and index2.
You can assume that each input Only corresponding to the only answer , And you Can not be Reuse the same elements .
The solution you design must use only constant level extra space .
Example 1:
Input :numbers = [2,7,11,15], target = 9
Output :[1,2]
explain :2 And 7 The sum is equal to the number of targets 9 . therefore index1 = 1, index2 = 2 . return [1, 2] .
Example 2:
Input :numbers = [2,3,4], target = 6
Output :[1,3]
explain :2 And 4 The sum is equal to the number of targets 6 . therefore index1 = 1, index2 = 3 . return [1, 3] .
Example 3:
Input :numbers = [-1,0], target = -1
Output :[1,2]
explain :-1 And 0 The sum is equal to the number of targets -1 . therefore index1 = 1, index2 = 2 . return [1, 2] .
Tips :
2 <= numbers.length <= 3 * 104
-1000 <= numbers[i] <= 1000
numbers Press Non decreasing order array
-1000 <= target <= 1000
There is only one valid answer
Answer key
Method 1 : Double pointer
The optimal
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
// Double pointer Time complexity 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};
Method 2 : Two points search
// Two points search
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};
}
// This is stupid , Use the one above
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};
}
};
边栏推荐
- EXIT中断详解
- Xiaolu Inn - Trailer
- 冒泡排序 C语言
- 阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂
- 365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
- 千人规模互联网公司研发效能成功之路
- 牛客网编程题—【WY22 Fibonacci数列】和【替换空格】详解
- Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories
- webgl1.0下texture2D和texture2DProj区别
- AD常用快捷键
猜你喜欢
随机推荐
科班同学真的了解未来的职业规划吗?
Teardown 解除时间限制的方法
阿里云架构师细说游戏行业九大趋势
JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development
英伟达周锡健:设计到数字营销的最后一公里
Thousands of databases, physical machines all over the country, JD logistics full volume cloud live record | interview with excellent technical team
The road to success in R & D efficiency of 1000 person Internet companies
D3d Shader Instruction
51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
AI应用第一课:C语言支付宝刷脸登录
牛客网编程题—【WY22 Fibonacci数列】和【替换空格】详解
365天挑战LeetCode1000题——Day 035 每日一题 + 二分查找 13
Visual Basic .Net 如何获取命令参数
副作用和序列点
预约中,2022京东云产业融合新品发布会线上开启
Helm chart for Kubernetes
D3d Shader Instruction
With frequent data leakage and deletion events, how should enterprises build a security defense line?
容器安全开源检测工具--问脉 VeinMind(镜像后门、恶意样本、敏感信息、弱口令等)
题解:在一个排序数组中查找元素第一个和最后一个的位置 (个人笔记)









