当前位置:网站首页>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
边栏推荐
- 抑郁症治疗的进展
- Sword finger offer II 019 Delete at most one character to get palindrome (simple)
- MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
- 51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
- 线程同步、生命周期
- 你還不知道責任鏈模式的使用場景嗎?
- Arduino for esp8266串口功能简介
- AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
- 阿里研发三面,面试官一套组合拳让我当场懵逼
- qt之.pro文件详解
猜你喜欢
Parameter stack pressing problem of C language in structure parameter transmission
一款不错的工具:aardio
MySQL报错packet out of order
外包幹了四年,廢了...
[jz46 translate numbers into strings]
AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
【NLP】HMM隐马尔可夫+维特比分词
OpenFaaS实战之四:模板操作(template)
ASEMI整流模块MDQ100-16在智能开关电源中的作用
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
随机推荐
MySQL报错packet out of order
Go basic reflection
Svn detailed use tutorial
We reference My97DatePicker to realize the use of time plug-in
Resolve the conflict between computed attribute and input blur event
想要成为架构师?夯实基础最重要
A good tool: aardio
Realization of four data flow modes of grpc based on Multilingual Communication
One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
8.5 循环神经网络简洁实现
Bingbing learning notes: take you step by step to realize the sequence table
电容
压缩映射定理
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
Find daffodils - for loop practice
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
拼接hql时,新增字段没有出现在构造方法中
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
UML project example -- UML diagram description of tiktok
2-GO variable operation