当前位置:网站首页>16. sum of the nearest three numbers
16. sum of the nearest three numbers
2022-06-12 05:24:00 【anieoo】
Original link :16. The closest sum of three
solution:
This question is very similar to the sum of three numbers , In calculating the closest target When , Greater than target And less than target Two emotions
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()); // Sort
for(int i = 0;i < n;i++) {
if(i > 0 && nums[i] == nums[i - 1]) continue; // duplicate removal
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--;
// Calculate once greater than target The sum of the number of is less than target Number of numbers
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;
}
};边栏推荐
- Link: fatal error lnk1168: cannot open debug/test Solution of exe for writing
- Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)
- Overview of common classes
- How to deploy dolphin scheduler 1.3.1 on cdh5
- 第五讲:数据仓库搭建(三)
- Please remove any half-completed changes then run repair to fix the schema history
- [C language] realize string interception function
- It costs less than 30 yuan, but we still don't build it quickly - check the small knowledge of software application
- 17. print from 1 to the maximum n digits
- JS controls the display and hiding of tags through class
猜你喜欢

Codec of ASoC framework driven by alsa

Surface net radiation flux data, solar radiation data, rainfall data, air temperature data, sunshine duration, water vapor pressure distribution, wind speed and direction data, surface temperature

Detailed analysis of the 2021 central China Cup Title A (color selection of mosaic tiles)

Performance test - performance test tool analysis

Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)
![[GIS tutorial] land use transfer matrix](/img/89/c5b55262e39405547c46538355e278.jpg)
[GIS tutorial] land use transfer matrix

Test work summary - performance test indicators

BI 如何让SaaS产品具有 “安全感”和“敏锐感”(上)

Main business objects of pupanvr record (5)

Sentinel-2 data introduction and download
随机推荐
The combined application of TOPSIS and fuzzy borde (taking the second Dawan District cup and the national championship as examples, it may cause misunderstanding, and the Dawan District cup will be up
Pupanvr hardware and software board side development environment configuration (4)
Platform of ASoC framework driven by alsa
SQL transaction
Nature | 给全球的新冠伤亡算一笔账
Spatial distribution data of national multi-year average precipitation 1951-2021, temperature distribution data, evapotranspiration data, evaporation data, solar radiation data, sunshine data and wind
Simulation of array implementation stack
Set common methods
BI 如何让SaaS产品具有 “安全感”和“敏锐感”(上)
38. arrangement of strings
Development of video preview for main interface of pupanvr-ui
31. stack push in and pop-up sequence
How to generate provincial data from county-level data in ArcGIS?
WiFi smartconfig implementation
Pupanvr- an open source embedded NVR system (1)
Vivado HLS introductory notes
Detailed analysis of the 2021 central China Cup Title A (color selection of mosaic tiles)
Three. JS import model demo analysis (with notes)
Nbiot module me3616 at command mqtt connecting thingsboard
Minigui3 runs on Hisilicon hi3520d/hi3531 platform