当前位置:网站首页>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)
边栏推荐
- Lean product development - Lean Software Development & lean product development
- RTKLIB: demo5 b34f.1 vs b33
- FGUI工程打包发布&导入Unity&将UI显示出来的方式
- Pride-pppar source code analysis
- RTKLIB: demo5 b34f. 1 vs b33
- 使用rtknavi进行RT-PPP测试
- 10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
- 121道分布式面试题和答案
- Employment of cashier [differential constraint]
- FairyGUI循環列錶
猜你喜欢
Fairygui bar subfamily (scroll bar, slider, progress bar)
Role movement in the first person perspective
系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
基本Dos命令
Chromatic judgement bipartite graph
Application architecture of large live broadcast platform
Dark chain lock (lca+ difference on tree)
第一人称视角的角色移动
Fairygui loop list
面试必备:聊聊分布式锁的多种实现!
随机推荐
Several high-frequency JVM interview questions
记录:newInstance()过时的代替方法
[算法] 劍指offer2 golang 面試題2:二進制加法
Compile GDAL source code with nmake (win10, vs2022)
Mixed use of fairygui button dynamics
Fairygui bar subfamily (scroll bar, slider, progress bar)
Sharing ideas of on-chip transplantation based on rtklib source code
Chromatic judgement bipartite graph
系统设计学习(一)Design Pastebin.com (or Bit.ly)
基本Dos命令
阿里云微服务(三)Sentinel开源流控熔断降级组件
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
Pride-pppar source code analysis
【RTKLIB 2.4.3 b34 】版本更新简介一
地球围绕太阳转
How to reduce the shutdown time of InnoDB database?
Excel导入,导出功能实现
Record: newinstance() obsolete replacement method
堆排序【手写小根堆】
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing