当前位置:网站首页>[leetcode16] the sum of the nearest three numbers (double pointer)
[leetcode16] the sum of the nearest three numbers (double pointer)
2022-07-06 05:02:00 【Evening scenery at the top of the mountain】
One 、 subject
Two 、 Ideas
and leetcode15 The sum of three is similar , Three level cycle time complexity O ( n 3 ) O(n^3) O(n3) Too high , Use double pointer, although it can't drop to O ( n ) O(n) O(n), But it is the common routine of this kind of questions :
- Go through the array elements , The current element is
num[i]
; - Set the left and right pointers , The right pointer is the right endpoint , Note that the left pointer is
left + 1
, Not from 0 Start , Otherwise, there will be repeated elements due to repeated traversal ; When the sum of three numbers is greater thantarget
beright
Move left to reduce the value . - Every time we judge the current combination of three numbers , That is, calculate and target Difference between , If it is lower than the current minimum difference
minAbsError
Still small , You can update .
3、 ... and 、 Code
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int ans = 0, left = 0, right = 0;
int minAbsError = INT_MAX, error = 0, absError = 0, temp = 0;
for(int i = 0; i < nums.size() - 2; i++){
left = i + 1;
right = nums.size() - 1;
while(left < right){
temp = nums[i] + nums[left] + nums[right];
error = temp - target;
absError = abs(error);
if(minAbsError > absError){
// The update process
ans = temp;
minAbsError = absError;
}else if(error > 0){
right--;
}else{
left++;
}
}
}
return ans;
}
};
边栏推荐
- 2021robocom robot developer competition (Preliminary)
- Postman Association
- [FreeRTOS interrupt experiment]
- Finance online homework
- Flody的应用
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Set detailed map + interview questions
- Idea one key guide package
- RT thread analysis - object container implementation and function
猜你喜欢
Postman前置脚本-全局变量和环境变量
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Idea one key guide package
Uva1592 Database
Rce code and Command Execution Vulnerability
Postman关联
Orm-f & Q object
The IPO of mesk Electronics was terminated: Henan assets, which was once intended to raise 800 million yuan, was a shareholder
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
Acwing week 58
随机推荐
驱动开发——第一个HelloDDK
Rce code and Command Execution Vulnerability
Basic knowledge and examples of binary tree
It is also a small summary in learning
Platformio create libopencm3 + FreeRTOS project
Imperial cms7.5 imitation "D9 download station" software application download website source code
Driver development - hellowdm driver
关于Unity Inspector上的一些常用技巧,一般用于编辑器扩展或者其他
Excellent PM must experience these three levels of transformation!
Finance online homework
IPv6 comprehensive experiment
团队协作出了问题,项目经理怎么办?
Set detailed map + interview questions
Postman测试报告
RTP GB28181 文件测试工具
程序员在互联网行业的地位 | 每日趣闻
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
Codeforces Round #804 (Div. 2)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
[lgr-109] Luogu may race II & windy round 6