当前位置:网站首页>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
边栏推荐
- Realization of four data flow modes of grpc based on Multilingual Communication
- 你還不知道責任鏈模式的使用場景嗎?
- Programming philosophy - automatic loading, dependency injection and control inversion
- 《JVM系列》 第七章 -- 字节码执行引擎
- AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
- 51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
- 在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
- 中富金石财富班29800效果如何?与专业投资者同行让投资更简单
- vscode中文插件不生效问题解决
- 8.3 语言模型与数据集
猜你喜欢
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
阿里研发三面,面试官一套组合拳让我当场懵逼
1n5408-asemi rectifier diode
LeetCode 练习——396. 旋转函数
ArrayList collection basic usage
555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
线程同步、生命周期
51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
三、梯度下降求解最小θ
外包干了四年,废了...
随机推荐
eolink 如何助力远程办公
L'externalisation a duré quatre ans.
【NLP】HMM隐马尔可夫+维特比分词
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
1 - first knowledge of go language
LeetCode167-两数之和II-双指针-二分-数组-查找
Detailed explanation of C language P2 selection branch statement
1N5408-ASEMI整流二极管1N5408
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
MDS55-16-ASEMI整流模块MDS55-16
LeetCode151-颠倒字符串中的单词-字符串-模拟
科技的成就(二十一)
QT actual combat: Yunxi calendar
UML项目实例——抖音的UML图描述
三、梯度下降求解最小θ
Select receives both normal data and out of band data
LeetCode162-寻找峰值-二分-数组
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program