当前位置:网站首页>167. Sum of two numbers II - input ordered array - Double pointers
167. Sum of two numbers II - input ordered array - Double pointers
2022-07-06 13:05:00 【The_ Dan】
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int left = 0, right = numbers.size() - 1;
while(left < right){
if(numbers[left] + numbers[right] == target)
return {
left + 1, right + 1};
while(numbers[left] + numbers[right] > target) // use while Can improve efficiency
right--;
while(numbers[left] + numbers[right] < target)
left++;
}
return {
-1, -1};
}
};
Accepted
21/21 cases passed (8 ms)
Your runtime beats 28.77 % of cpp submissions
Your memory usage beats 5.03 % of cpp submissions (15.3 MB)
边栏推荐
- [algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
- Code example of MATLAB reading GNSS observation value o file
- [算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
- Agile development helps me
- 分支语句和循环语句
- On March 15, the official version of go 1.18 was released to learn about the latest features and usage
- 阿里云微服务(四) Service Mesh综述以及实例Istio
- The earth revolves around the sun
- How to improve the deletion speed of sequential class containers?
- Role movement in the first person perspective
猜你喜欢

地球围绕太阳转
![[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组](/img/65/fc3fb5a217a3b44f506b695af53e2c.png)
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组

Application architecture of large live broadcast platform

系统设计学习(一)Design Pastebin.com (or Bit.ly)
![[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数](/img/64/0f352232359c7d44f12b20a64c7bb4.png)
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数

十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩

Several high-frequency JVM interview questions

Liste des boucles de l'interface graphique de défaillance

【无标题】

KF UD分解之UD分解基础篇【1】
随机推荐
WSL common commands
国企秋招经验总结
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
2022 National Games RE1 baby_ tree
Novatel board oem617d configuration step record
如何保障 MySQL 和 Redis 的数据一致性?
【GNSS】抗差估计(稳健估计)原理及程序实现
FairyGUI循環列錶
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
FairyGUI条子家族(滚动条,滑动条,进度条)
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
Compile GDAL source code with nmake (win10, vs2022)
GNSS positioning accuracy index calculation
[GNSS] robust estimation (robust estimation) principle and program implementation
阿里云微服务(三)Sentinel开源流控熔断降级组件
[算法] 剑指offer2 golang 面试题2:二进制加法
Pride-pppar source code analysis
Music playback (toggle & playerprefs)
继承和多态(下)