当前位置:网站首页>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
边栏推荐
- QT actual combat: Yunxi calendar
- PCIe X1 插槽的主要用途是什么?
- Programming philosophy - automatic loading, dependency injection and control inversion
- eolink 如何助力远程办公
- Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
- [proteus simulation] automatic range (range < 10V) switching digital voltmeter
- 阿里研发三面,面试官一套组合拳让我当场懵逼
- Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
- LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)
- One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
猜你喜欢
三、梯度下降求解最小θ
What is the main purpose of PCIe X1 slot?
Find daffodils - for loop practice
The art of automation
阿里研发三面,面试官一套组合拳让我当场懵逼
【Servlet】Servlet 详解(使用+原理)
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
想要成为架构师?夯实基础最重要
Introduction to Arduino for esp8266 serial port function
随机推荐
编程哲学——自动加载、依赖注入与控制反转
One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
外包干了四年,废了...
we引用My97DatePicker 实现时间插件使用
What is the main purpose of PCIe X1 slot?
金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
2-GO variable operation
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
Vscode Chinese plug-in doesn't work. Problem solving
Master in minutes --- ternary operator (ternary operator)
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
SQL中HAVING和WHERE的区别
Sword finger offer II 019 Delete at most one character to get palindrome (simple)
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
如何打开Win10启动文件夹?
QT interface optimization: double click effect
3、 Gradient descent solution θ
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another