当前位置:网站首页>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
边栏推荐
- 成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
- [proteus simulation] automatic range (range < 10V) switching digital voltmeter
- 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)
- 自动化的艺术
- 机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
- capacitance
- 压缩映射定理
- Progress in the treatment of depression
- I/O复用的高级应用:同时处理 TCP 和 UDP 服务
- we引用My97DatePicker 实现时间插件使用
猜你喜欢
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
Don't you know the usage scenario of the responsibility chain model?
QT interface optimization: QT border removal and form rounding
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
编程哲学——自动加载、依赖注入与控制反转
LeetCode165-比较版本号-双指针-字符串
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
Branch statement of process control
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
8.3 语言模型与数据集
随机推荐
vscode中文插件不生效问题解决
Mds55-16-asemi rectifier module mds55-16
Parameter stack pressing problem of C language in structure parameter transmission
capacitance
DVWA之暴力破解(Brute Force)Low-->high
We reference My97DatePicker to realize the use of time plug-in
[detailed explanation of factory mode] factory method mode
PCIe X1 插槽的主要用途是什么?
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
8.2 文本预处理
LeetCode165-比较版本号-双指针-字符串
select 同时接收普通数据 和 带外数据
1N5408-ASEMI整流二极管1N5408
Pnpm installation and use
Want to be an architect? Tamping the foundation is the most important
【Proteus仿真】自动量程(范围<10V)切换数字电压表
qt之.pro文件详解
【Servlet】Servlet 详解(使用+原理)
Resolve the conflict between computed attribute and input blur event
Thread synchronization, life cycle