当前位置:网站首页>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;
}
};边栏推荐
- National land use data of 30m precision secondary classification
- Thingsboard view telemetry data through database
- 67. convert string to integer
- How to quickly reference uview UL in uniapp, and introduce and use uviewui in uni app
- Some optimization methods for UI Application of Qt5 on Hisilicon security platform
- Applet pull-down load refresh onreachbottom
- Servlet core
- [GIS tutorial] land use transfer matrix
- Please remove any half-completed changes then run repair to fix the schema history
- JS how to get the date
猜你喜欢

Nature | 给全球的新冠伤亡算一笔账

Vivado HLS introductory notes

Sentinel-2 data introduction and download

Matlab: image rotation and interpolation and comparison of MSE before and after

Multi thread learning v. volatile visibility and cache inconsistency, instruction reordering

2022 "college entrance examination memory" has been packaged, please check!

How to quickly reference uview UL in uniapp, and introduce and use uviewui in uni app

Servlet core technology

Detailed tutorial on the use of yolov5 and training your own dataset with yolov5

4.3 simulate browser operation and page waiting (display waiting and implicit waiting, handle)
随机推荐
Development of video preview for main interface of pupanvr-ui
Ray. Tune visual adjustment super parameter tensorflow 2.0
Link: fatal error lnk1168: cannot open debug/test Solution of exe for writing
Matlab: halftone and dither conversion
Detailed usage of vim editor
Codec of ASoC framework driven by alsa
[backtracking method] backtracking method to solve the problem of Full Permutation
The most commonly used objective weighting method -- entropy weight method
Some problems of silly girl solved
4.3 simulate browser operation and page waiting (display waiting and implicit waiting, handle)
Stm32f4 ll library multi-channel ADC
Realease package appears – missing type parameter
Applet pull-down load refresh onreachbottom
[cjson] precautions for root node
Chapter 1
Static keyword and inheritance, polymorphic and special classes
Detailed analysis of mathematical modeling problem a (vaccine production scheduling problem) of May Day cup in 2021
Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class
Qs100 at command mqtt access thingsboard
[GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)