当前位置:网站首页>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 8: the shortest subarray with a sum greater than or equal to K
- Error: symbol not found
- MySQL backup -- common errors in xtrabackup backup
- [rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
- Role movement in the first person perspective
- GPS高程拟合抗差中误差的求取代码实现
- 阿里云一面:并发场景下的底层细节 - 伪共享问题
- [算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
- Fairygui bar subfamily (scroll bar, slider, progress bar)
- 十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
猜你喜欢
How do architects draw system architecture blueprints?
一文搞定 UDP 和 TCP 高频面试题!
String类
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
Prove the time complexity of heap sorting
121 distributed interview questions and answers
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
阿里云微服务(四) Service Mesh综述以及实例Istio
Iterable、Collection、List 的常见方法签名以及含义
Heap sort [handwritten small root heap]
随机推荐
String类
2-year experience summary, tell you how to do a good job in project management
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
[algorithm] sword finger offer2 golang interview question 2: binary addition
Matlab读取GNSS 观测值o文件代码示例
堆排序【手写小根堆】
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
记录:Navicat Premium初次无法连接数据库MySQL之解决
Excel导入,导出功能实现
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
记录:下一不小心写了个递归
2022 National Games RE1 baby_ tree
Usage differences between isempty and isblank
MySQL shutdown is slow
阿里云微服务(四) Service Mesh综述以及实例Istio
如何保障 MySQL 和 Redis 的数据一致性?
Implementation of Excel import and export functions
Experience summary of autumn recruitment of state-owned enterprises
RTKLIB: demo5 b34f.1 vs b33
Several high-frequency JVM interview questions