当前位置:网站首页>16. 最接近的三數之和
16. 最接近的三數之和
2022-06-12 05:24:00 【anieoo】
原題鏈接:16. 最接近的三數之和
solution:
本題和三數之和很相似,在計算最接近target的時候,需要考慮大於target和小於target兩種情
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int n = nums.size();
pair<int, int> res(INT_MAX, INT_MAX);
sort(nums.begin(),nums.end()); //排序
for(int i = 0;i < n;i++) {
if(i > 0 && nums[i] == nums[i - 1]) continue; //去重
for(int j = i + 1,k = n - 1;j < k;j++) {
if(j > i + 1 && nums[j] == nums[j - 1]) continue;
while(k - 1 > j && nums[i] + nums[j] + nums[k - 1] > target) k--;
//計算一次大於target的數和一次小於target的數
int sum = nums[i] + nums[j] + nums[k];
res = min(res, make_pair(abs(target - sum), sum));
if(k - 1 > j) {
sum = nums[i] + nums[j] + nums[k - 1];
res = min(res, make_pair(abs(target - sum), sum));
}
}
}
return res.second;
}
};边栏推荐
- org. apache. ibatis. binding. BindingException: Invalid bound statement (not found)
- Calculation method notes for personal use
- 43. Number of occurrences of 1 in 1 ~ n integers
- Test work summary - performance test related issues
- National land use data of 30m precision secondary classification
- 4.3 模拟浏览器操作和页面等待(显示等待和隐式等待、句柄)
- 4.3 simulate browser operation and page waiting (display waiting and implicit waiting, handle)
- Codec of ASoC framework driven by alsa
- Radiometric calibration and atmospheric correction of sentry 2 L1C multispectral data using sen2cor
- Some problems of silly girl solved
猜你喜欢

Servlet core

20000 word detailed reptile knowledge reserve, basic exercises of data collection and cleaning (I) reference answers to the first song

Transpiration and evapotranspiration (ET) data, potential evapotranspiration, actual evapotranspiration data, temperature data, rainfall data
![[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e](/img/2e/97310ec36aeb1fc1e9c82361141a36.jpg)
[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e

SQL transaction

Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)

Performance test - Analysis of performance test results

Selenium crawler automatically captures TOEFL test position of NEEA website

Quickly get PCA (principal component analysis) (principle code case)

Yolo opencv scale identification scale reading identification water gauge identification water level identification source code
随机推荐
JS how to get the date
2022 "college entrance examination memory" has been packaged, please check!
12.24 day exercise -- Programming summation, 99 multiplication table, while loop and for loop exercises
Pytorch was reported by a large number of netizens that torchrec, a new library, was "born" and has a large scale
Deploying a single node kubernetes cluster using rancher-2.5.5
Font conversion optimization
When the build When gradle does not load the dependencies, and you need to add a download path in libraries, the path in gradle is not a direct downloadable path
Project requirements specification
Matlab: image rotation and interpolation and comparison of MSE before and after
Quickly get PCA (principal component analysis) (principle code case)
Multi thread learning v. volatile visibility and cache inconsistency, instruction reordering
Pupanvr create project and management script (2)
One dragon and one knight accompanying programmers are 36 years old
BI 如何让SaaS产品具有 “安全感”和“敏锐感”(上)
Qs100 at command mqtt access thingsboard
Yolov5 realizes road crack detection
什么是工程预付款
Codec of ASoC framework driven by alsa
IC验证中的force/release 学习整理(6)研究对 wire 类型信号的影响
29. print matrix clockwise