当前位置:网站首页>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
边栏推荐
- Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
- 1-初识Go语言
- DVWA之暴力破解(Brute Force)Low-->high
- Bingbing learning notes: take you step by step to realize the sequence table
- 外包干了四年,废了...
- Chapter 7 of JVM series -- bytecode execution engine
- QT actual combat: Yunxi chat room
- [proteus simulation] automatic range (range < 10V) switching digital voltmeter
- The art of automation
- capacitance
猜你喜欢

详解TCP的三次握手

DVWA之暴力破解(Brute Force)Low-->high

Find daffodils - for loop practice

Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"

抑郁症治疗的进展

Model location setting in GIS data processing -cesium

UML项目实例——抖音的UML图描述

1-初识Go语言

LeetCode 练习——396. 旋转函数
![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]
随机推荐
ASEMI整流模块MDQ100-16在智能开关电源中的作用
Swift protocol Association object resource name management multithreading GCD delay once
Don't you know the usage scenario of the responsibility chain model?
Parameter stack pressing problem of C language in structure parameter transmission
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
PCIe X1 插槽的主要用途是什么?
[proteus simulation] automatic range (range < 10V) switching digital voltmeter
8.3 语言模型与数据集
Go basic reflection
2-Go变量操作
LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
select 同时接收普通数据 和 带外数据
LeetCode151-颠倒字符串中的单词-字符串-模拟
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
8.4 循环神经网络从零实现
UML项目实例——抖音的UML图描述
LeetCode167-两数之和II-双指针-二分-数组-查找
First acquaintance with STL
A blog allows you to learn how to write markdown on vscode
