当前位置:网站首页>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;
}
};边栏推荐
- Why is Julia so popular?
- Introduction to MMS memory optimization of Hisilicon MPP service
- How to quickly reference uview UL in uniapp, and introduce and use uviewui in uni app
- How to deploy dolphin scheduler Apache dolphin scheduler 1.2.0 in cdh5.16.2
- 【C语言】实现字符串截取功能
- Alsa architecture application aplay and amixer call relationship (from application layer to kernel driver)
- Index fund summary
- What is thinking
- [cjson] precautions for root node
- [C language] realize string interception function
猜你喜欢
![[GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)](/img/60/baebffb2024ddf5f2cb070f222b257.jpg)
[GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)

Vivado HLS introductory notes

Abstract methods and interfaces

Test work summary - performance test indicators

One dragon and one knight accompanying programmers are 36 years old

Drive safety coding & troubleshooting guide

Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)
![February 19, 2022 [Nolan] Nolan resurrected? Change · Nolan [soul orchid] can be connected to XDD / silly girl](/img/1a/ab2158a532683632f3a12fe41812f5.jpg)
February 19, 2022 [Nolan] Nolan resurrected? Change · Nolan [soul orchid] can be connected to XDD / silly girl

Project requirements specification

Computer network connected but unable to access the Internet
随机推荐
BI 如何让SaaS产品具有 “安全感”和“敏锐感”(上)
JS to determine whether the tags of multiple classes are empty
67. convert string to integer
Three. JS import model demo analysis (with notes)
[backtracking method] backtracking method to solve the problem of Full Permutation
Applet pull-down load refresh onreachbottom
JS set the position of the current scroll bar
Design of a simple embedded web service application
Some problems of Qinglong panel
【cjson】根节点注意事项
MySQL5.7.21 Build For ARM
CentOS compiling and installing mysql8.0
Vivado HLS introductory notes
Pupanvr create project and management script (2)
A complete set of installation procedures (for learning and communication only)
43. Number of occurrences of 1 in 1 ~ n integers
Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)
Automated testing - Po mode / log /allure/ continuous integration
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
Sv806 QT UI development