当前位置:网站首页>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;
}
};边栏推荐
- New knowledge today
- Some problems of Qinglong panel
- It costs less than 30 yuan, but we still don't build it quickly - check the small knowledge of software application
- @Configurationproperties value cannot be injected
- Set common methods
- 49. ugly number
- 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
- 44. a digit in a sequence of digits
- 4.3 simulate browser operation and page waiting (display waiting and implicit waiting, handle)
- 2022 "college entrance examination memory" has been packaged, please check!
猜你喜欢

The most commonly used objective weighting method -- entropy weight method

Thingsboard create RCP widget

Microsoft announces that it will discontinue support for older versions of visual studio
![[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

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

Classes and objects, methods and encapsulation

Token based authentication

How to deploy dolphin scheduler Apache dolphin scheduler 1.2.0 in cdh5.16.2

Big manufacturers compete to join rust, performance and safety are the key, and the 2021 rust developer survey report is announced

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
随机推荐
Layer sublayer assigns values to the page elements of the parent layer to achieve the effect of transferring values to the page of the parent layer
Set common methods
29. print matrix clockwise
WiFi smartconfig implementation
February 19, 2022 [Nolan] Nolan resurrected? Change · Nolan [soul orchid] can be connected to XDD / silly girl
Google reinforcement learning framework seed RL environment deployment
IC验证中的force/release 学习整理(6)研究对 wire 类型信号的影响
Automated testing - Po mode / log /allure/ continuous integration
[backtracking method] queen n problem
New knowledge today
[C language] realize string interception function
National land use data of 30m precision secondary classification
51. reverse order pairs in the array
JS how to get the date
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
Pupanvr hardware and software board side development environment configuration (4)
Self implementation of a UI Library - UI core drawing layer management
Chrome is amazingly fast, fixing 40 vulnerabilities in less than 30 days
Test work summary - performance test indicators
17. print from 1 to the maximum n digits