当前位置:网站首页>[sum of two numbers] 169 sum of two numbers II - enter an ordered array
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
2022-07-05 05:16:00 【lee2813】
One 、 subject
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] .
Two 、 Answer key
This problem is to traverse an array , And find two elements , It's not an interval , And the array is ordered , Therefore, the double pointer method can be used .
- First initialize the double pointer , Point to the smallest position and the largest position respectively , The minimum position pointer traverses to the right , The maximum position pointer traverses to the left .
- In the process of traversing , There are three situations (1) Sum of two numbers > The goal is : The maximum position pointer moves to the left ;2) Sum of two numbers < The goal is : The minimum position pointer moves to the right ;3) Sum of two numbers = The goal is : Successfully found
Be careful : In the opposite direction, pay attention to the boundary conditions , Here are two pointers pointing to the same position
3、 ... and 、 Code
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int s = 0;
int f = numbers.size()-1;
while(s!=f){
if((numbers[s] + numbers[f])==target) break;
else if((numbers[s] + numbers[f])>target) f--;
else s++;
}
return vector<int> {
s+1,f+1};
}
};
边栏推荐
- Ue4/ue5 illusory engine, material part (III), material optimization at different distances
- [轉]: OSGI規範 深入淺出
- 使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
- 2022/7/1 learning summary
- A three-dimensional button
- Bucket sort
- Pause and resume of cocos2dx Lua scenario
- 《动手学深度学习》学习笔记
- [turn to] MySQL operation practice (III): table connection
- Lua GBK and UTF8 turn to each other
猜你喜欢
![[轉]: OSGI規範 深入淺出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[轉]: OSGI規範 深入淺出

Learning notes of "hands on learning in depth"

Bucket sort

669. Prune binary search tree ●●

Do a small pressure test with JMeter tool

Applet live + e-commerce, if you want to be a new retail e-commerce, use it!

National teacher qualification examination in the first half of 2022

3dsmax snaps to frozen objects

3dsmax scanning function point connection drawing connection line

UE fantasy engine, project structure
随机推荐
Forecast report on research and investment prospects of Chinese wormwood industry (2022 Edition)
[轉]: OSGI規範 深入淺出
Solon 框架如何方便获取每个请求的响应时间?
Es module and commonjs learning notes
Simple modal box
C # perspective following
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
UE 虚幻引擎,项目结构
Page countdown
小程序直播+电商,想做新零售电商就用它吧!
How much do you know about 3DMAX rendering skills and HDRI light sources? Dry goods sharing
3dsmax2018 common operations and some shortcut keys of editable polygons
Research on the value of background repeat of background tiling
Time format conversion
xftp7与xshell7下载(官网)
一个新的微型ORM开源框架
[trans]: spécification osgi
Collapse of adjacent vertical outer margins
Simple HelloWorld color change
Redis has four methods for checking big keys, which are necessary for optimization