当前位置:网站首页>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
边栏推荐
- Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
- What is the main purpose of PCIe X1 slot?
- 1-初识Go语言
- 在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
- raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
- [detailed explanation of factory mode] factory method mode
- QT actual combat: Yunxi chat room
- 8.5 循环神经网络简洁实现
- 基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
- 编程哲学——自动加载、依赖注入与控制反转
猜你喜欢

AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
![Detailed explanation of C language knowledge points -- first knowledge of C language [1]](/img/7d/2fc527949562e1a7ef1525c7fced81.png)
Detailed explanation of C language knowledge points -- first knowledge of C language [1]

We reference My97DatePicker to realize the use of time plug-in

QT interface optimization: QT border removal and form rounding

Progress in the treatment of depression

Sword finger offer II 019 Delete at most one character to get palindrome (simple)

【JZ46 把数字翻译成字符串】

I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another

Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once

《JVM系列》 第七章 -- 字节码执行引擎
随机推荐
抑郁症治疗的进展
你還不知道責任鏈模式的使用場景嗎?
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
Vscode Chinese plug-in doesn't work. Problem solving
1 - first knowledge of go language
51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
GIS数据处理-cesium中模型位置设置
电容
冰冰学习笔记:一步一步带你实现顺序表
解决computed属性与input的blur事件冲突问题
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
select 同时接收普通数据 和 带外数据
[NLP] HMM hidden Markov + Viterbi word segmentation
Bingbing learning notes: take you step by step to realize the sequence table
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
QT actual combat: Yunxi calendar
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
