当前位置:网站首页>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
边栏推荐
- 一款不错的工具:aardio
- 面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
- 51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
- A good tool: aardio
- PCIe X1 插槽的主要用途是什么?
- 1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
- Introduction to Arduino for esp8266 serial port function
- [servlet] detailed explanation of servlet (use + principle)
- How do I open the win10 startup folder?
- Go basic reflection
猜你喜欢
详解TCP的三次握手
Chapter 7 of JVM series -- bytecode execution engine
QT Detailed explanation of pro file
Brute force of DVWA low -- > High
ASEMI超快恢复二极管与肖特基二极管可以互换吗
QT actual combat: Yunxi chat room
【无标题】
555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
随机推荐
3、 Gradient descent solution θ
Explanation and example application of the principle of logistic regression in machine learning
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
Arduino for esp8266串口功能简介
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
Bingbing learning notes: take you step by step to realize the sequence table
Branch statement of process control
外包干了四年,废了...
Pnpm installation and use
8.3 语言模型与数据集
【工厂模式详解】工厂方法模式
How do I open the win10 startup folder?
I/O复用的高级应用:同时处理 TCP 和 UDP 服务
Outsourcing for four years, abandoned
LeetCode 练习——396. 旋转函数
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
[servlet] detailed explanation of servlet (use + principle)
1 - first knowledge of go language