当前位置:网站首页>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
边栏推荐
- 1n5408-asemi rectifier diode
- Is asemi ultrafast recovery diode interchangeable with Schottky diode
- do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
- LeetCode151-颠倒字符串中的单词-字符串-模拟
- Explanation and example application of the principle of logistic regression in machine learning
- 你还不知道责任链模式的使用场景吗?
- LeetCode165-比较版本号-双指针-字符串
- Detailed explanation of C language P2 selection branch statement
- Realization of four data flow modes of grpc based on Multilingual Communication
- 3、 Gradient descent solution θ
猜你喜欢
随机推荐
Brute force of DVWA low -- > High
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
外包干了四年,废了...
8.5 循环神经网络简洁实现
【JZ46 把数字翻译成字符串】
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
电容
ASEMI超快恢复二极管与肖特基二极管可以互换吗
Comment eolink facilite le télétravail
Parameter stack pressing problem of C language in structure parameter transmission
【NLP】HMM隐马尔可夫+维特比分词
Select receives both normal data and out of band data
1-初识Go语言
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
解决computed属性与input的blur事件冲突问题
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
如何打开Win10启动文件夹?
ArrayList collection basic usage
Achievements in science and Technology (21)