当前位置:网站首页>(practice C language every day) the sum of the nearest three numbers
(practice C language every day) the sum of the nearest three numbers
2022-07-02 16:05:00 【Yi Xiaoxia】
The closest sum of three
Given an include n Array of integers nums and A target value target. find nums Three integers in , Make their sum and target Nearest . Returns the sum of the three numbers . Assume that there is only one answer for each group of input .
Example :
Input :nums = [-1,2,1,-4], target = 1 Output :2 explain : And target The closest and 2 (-1 + 2 + 1 = 2) .
Tips :
3 <= nums.length <= 10^3-10^3 <= nums[i] <= 10^3-10^4 <= target <= 10^4
The following program realizes this function :
#include <cstdlib>
class Solution
{
public:
int threeSumClosest(vector<int> &nums, int target)
{
sort(nums.begin(), nums.end());
int cur, left, right;
cur = 0;
int closest = nums[0] + nums[1] + nums[2];
while (cur < nums.size() - 2)
{
left = cur + 1;
right = nums.size() - 1;
int n;
while (left < right)
{
n = nums[cur] + nums[left] + nums[right];
if (abs(target - n) < abs(target - closest))
{
closest = n;
}
if (n == target)
{
break;
}
else if (n > target)
{
int t = right - 1;
while (t > left && nums[t] == nums[right])
t--;
right = t;
}
else
{
int t = left + 1;
while (t < right && nums[t] == nums[left])
t++;
left = t;
}
}
int t = cur + 1;
while (t < nums.size() && nums[t] == nums[cur])
t++;
cur = t;
}
return closest;
}
};边栏推荐
- In memory of becoming the first dayu200 tripartite demo contributor
- [2. Basics of Delphi grammar] 3 Object Pascal constants and variables
- mysql 计算经纬度范围内的数据
- 解决** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the defau
- 【题解】Educational Codeforces Round 82
- Ssh/scp does not prompt all activities are monitored and reported
- Tree binary search tree
- Demo of converting point cloud coordinates to world coordinates
- 将点云坐标转换成世界坐标的demo
- Dimension table and fact table in data warehouse
猜你喜欢

JS learning notes - variables

爱可可AI前沿推介(7.2)

Why does the system convert the temp environment variable to a short file name?

如何實現十億級離線 CSV 導入 Nebula Graph

基于 Nebula Graph 构建百亿关系知识图谱实践

Processing gzip: stdin: not in gzip format: child returned status 1tar: error is not recoverable: exitin

结构体的内存对齐

Comment réaliser un graphique Nebula d'importation CSV hors ligne de niveau milliard

Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau

Maui学习之路(三)--Winui3深入探讨
随机推荐
Solve the problem of base64encoder error
《大学“电路分析基础”课程实验合集.实验五》丨线性有源二端网络等效电路的研究
Song of cactus - throwing stones to ask the way (2)
Idea public method extraction shortcut key
Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC
Usage of group by
Ant group's large-scale map computing system tugraph passed the national evaluation
/Bin/ld: cannot find -llz4
HMS core machine learning service helps zaful users to shop conveniently
MySQL calculates the data within the longitude and latitude range
Maui学习之路(三)--Winui3深入探讨
Boot 中bean配置覆盖
By asp Net core downloads files according to the path exception
Nebula Graph & 数仓血缘关系数据的存储与读写
Golang MD5 encryption and MD5 salt value encryption
Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network
去除router-link中的下划线
idea jar包冲突排查
Tree binary search tree
Recommended practice sharing of Zhilian recruitment based on Nebula graph