当前位置:网站首页>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;
}
};边栏推荐
- [cjson] precautions for root node
- [C language] realize string interception function
- Thingsboard create RCP widget
- Force/release learning arrangement in IC Verification (5) research on the influence of reg type signals
- 37. serialized binary tree
- 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
- Detailed analysis of mathematical modeling problem a (vaccine production scheduling problem) of May Day cup in 2021
- ESP8266 Arduino OLED
- Simulation of array implementation stack
- Force/release learning and sorting in IC Verification (6) research on the influence of wire type signals
猜你喜欢

Yolov5 realizes road crack detection

Some problems of Qinglong panel

Project requirements specification

How to generate provincial data from county-level data in ArcGIS?

Transpiration and evapotranspiration (ET) data, potential evapotranspiration, actual evapotranspiration data, temperature data, rainfall data

It costs less than 30 yuan, but we still don't build it quickly - check the small knowledge of software application

Normalized vegetation index (NDVI) data, NPP data, GPP data, evapotranspiration data, vegetation type data, ecosystem type distribution data

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

Servlet core
![[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
随机推荐
[backtracking] backtracking to solve subset problems
Spatial distribution data of China's tertiary watershed / national new area distribution data /npp net primary productivity data / spatial distribution data of vegetation cover / land use data /ndvi d
Computer network connected but unable to access the Internet
Chrome is amazingly fast, fixing 40 vulnerabilities in less than 30 days
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
Nature | 给全球的新冠伤亡算一笔账
The master programmer "plays" a C program that is not like C
Microsoft announces that it will discontinue support for older versions of visual studio
Yolo opencv scale identification scale reading identification water gauge identification water level identification source code
SQL transaction
IC验证中的force/release 学习整理(5)研究对 reg类型信号的影响
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
14- II. Cutting rope II
12.26 exercise summary
Map coordinate conversion of Baidu map API
How to deploy dolphin scheduler Apache dolphin scheduler 1.2.0 in cdh5.16.2
44. a digit in a sequence of digits
19. regular expression matching
Multi thread learning v. volatile visibility and cache inconsistency, instruction reordering
Project requirements specification