当前位置:网站首页>Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
2022-04-23 14:49:00 【Li Fan, hurry up】
Note:
Enumerate each location , Then set a position and go to two points to find the next position
I wanted to make a judgment , See if the first number has exceeded target You don't have to change it , It turns out that there will be negative numbers , It's not easy to add , Don't prune at all
The code is as follows :
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
for(int i = 0; i < numbers.size() - 1; i ++){
int l = i + 1, r = numbers.size() - 1;
while(l < r){
int mid = l + r + 1 >> 1;
if(numbers[i] + numbers[mid] <= target) l = mid;
else r = mid - 1;
}
if(numbers[i] + numbers[l] == target) return {
i + 1, l + 1};
}
return {
};
}
};
版权声明
本文为[Li Fan, hurry up]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231447599582.html
边栏推荐
- How do I open the win10 startup folder?
- we引用My97DatePicker 实现时间插件使用
- eolink 如何助力遠程辦公
- thinkphp5+数据大屏展示效果
- Sword finger offer II 019 Delete at most one character to get palindrome (simple)
- 编程哲学——自动加载、依赖注入与控制反转
- Arduino for esp8266串口功能简介
- Branch statement of process control
- MySQL error packet out of order
- 51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
猜你喜欢
全连接层的作用是什么?
A blog allows you to learn how to write markdown on vscode
eolink 如何助力远程办公
[servlet] detailed explanation of servlet (use + principle)
三、梯度下降求解最小θ
编程哲学——自动加载、依赖注入与控制反转
QT Detailed explanation of pro file
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
MySQL error packet out of order
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
随机推荐
[NLP] HMM hidden Markov + Viterbi word segmentation
QT interface optimization: QT border removal and form rounding
1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
抑郁症治疗的进展
Swift protocol Association object resource name management multithreading GCD delay once
Select receives both normal data and out of band data
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
[untitled]
The art of automation
When splicing HQL, the new field does not appear in the construction method
1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
Vscode Chinese plug-in doesn't work. Problem solving
LeetCode165-比较版本号-双指针-字符串
LeetCode 练习——396. 旋转函数
Detailed explanation of C language knowledge points -- first knowledge of C language [1]
MySQL error packet out of order
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
Introduction to Arduino for esp8266 serial port function
【JZ46 把数字翻译成字符串】