当前位置:网站首页>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;
}
};边栏推荐
- Some problems of Qinglong panel
- Computer network connected but unable to access the Internet
- Simulation of array implementation stack
- The way to promote software test engineer
- [GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)
- Main business objects of pupanvr record (5)
- Detailed analysis of the 2021 central China Cup Title A (color selection of mosaic tiles)
- 公司注册认缴资金多久
- 什么是工程预付款
- Set common methods
猜你喜欢

Why is Julia so popular?

Detailed usage of vim editor
![[cjson] precautions for root node](/img/6c/4462bc8126935f00a5c64f18de13ea.png)
[cjson] precautions for root node

个体工商户是不是法人企业

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

Quickly get PCA (principal component analysis) (principle code case)

How to count the total length of roads in the region and draw data histogram

Thingsboard create RCP widget

Map coordinate conversion of Baidu map API

Abstract methods and interfaces
随机推荐
IC验证中的force/release 学习整理(6)研究对 wire 类型信号的影响
Transpiration and evapotranspiration (ET) data, potential evapotranspiration, actual evapotranspiration data, temperature data, rainfall data
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
[GIS tutorial] land use transfer matrix
How to clear floating, and how does it work?
Deploying a single node kubernetes cluster using rancher-2.5.5
4.3 模拟浏览器操作和页面等待(显示等待和隐式等待、句柄)
The most commonly used objective weighting method -- entropy weight method
The server time zone value ‘Ö Ð¹ ú±ê ×¼ ʱ ¼ ä‘ is unrecognized or represents more than one time zone. You
How to deploy dolphin scheduler 1.3.1 on cdh5
Main business objects of pupanvr record (5)
Harris corner detection principle-
MySQL Linux Installation mysql-5.7.24
个体工商户是不是法人企业
Index fund summary
44. a digit in a sequence of digits
Rv1109 lvgl UI development
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
Detailed analysis of mathematical modeling problem a (vaccine production scheduling problem) of May Day cup in 2021