当前位置:网站首页>LeetCode167-两数之和II-双指针-二分-数组-查找
LeetCode167-两数之和II-双指针-二分-数组-查找
2022-04-23 14:48:00 【李烦烦搞快点】
Note:
枚举每个位置,然后定了一个位置之后去二分找下一个位置
本来想加个判断,看看第一个数是不是已经超过了target就不用找了,结果发现会有负数,还不好加,干脆不要这个剪枝了
代码如下:
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 {
};
}
};
版权声明
本文为[李烦烦搞快点]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Mr_Ghost812/article/details/124355831
边栏推荐
- 【工厂模式详解】工厂方法模式
- Branch statement of process control
- 如何打开Win10启动文件夹?
- 1n5408-asemi rectifier diode
- select 同时接收普通数据 和 带外数据
- Brute force of DVWA low -- > High
- I/O复用的高级应用:同时处理 TCP 和 UDP 服务
- QT interface optimization: QT border removal and form rounding
- 在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
- Programming philosophy - automatic loading, dependency injection and control inversion
猜你喜欢
电容
Outsourcing for four years, abandoned
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
详解TCP的三次握手
ASEMI超快恢复二极管与肖特基二极管可以互换吗
Sword finger offer II 019 Delete at most one character to get palindrome (simple)
1-初识Go语言
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
随机推荐
全连接层的作用是什么?
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
QT actual combat: Yunxi calendar
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
Unity_代码方式添加绑定按钮点击事件
【工厂模式详解】工厂方法模式
8.3 语言模型与数据集
3、 Gradient descent solution θ
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
冰冰学习笔记:一步一步带你实现顺序表
Unity_ Code mode add binding button click event
8.2 文本预处理
Go basic reflection
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
Mds55-16-asemi rectifier module mds55-16
1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
PWM speed regulation control system of DC motor based on 51 single chip microcomputer (with complete set of data such as Proteus simulation + C program)
A good tool: aardio
Alexnet model
I/O复用的高级应用:同时处理 TCP 和 UDP 服务