当前位置:网站首页>(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;
}
};
边栏推荐
- Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network
- Ant group's large-scale map computing system tugraph passed the national evaluation
- Introduction to dynamic planning I, BFS of queue (70.121.279.200)
- Dimension table and fact table in data warehouse
- Boot transaction usage
- /Bin/ld: cannot find -lpam
- 死锁的条件及解决方法
- What are the necessary functions of short video app development?
- 隐藏在 Nebula Graph 背后的星辰大海
- After the win10 system is upgraded for a period of time, the memory occupation is too high
猜你喜欢
idea 公共方法抽取快捷键
The outline dimension function application of small motherboard
Dimension table and fact table in data warehouse
图数据库|Nebula Graph v3.1.0 性能报告
又是一年毕业季
win10系统升级一段时间后,内存占用过高
Recommended practice sharing of Zhilian recruitment based on Nebula graph
Mobile web development learning notes - Layout
Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics
处理gzip: stdin: not in gzip formattar: Child returned status 1tar: Error is not recoverable: exitin
随机推荐
How to use percona tool to add fields to MySQL table after interruption
Comprehensively interpret the background and concept of service mesh
Demo of converting point cloud coordinates to world coordinates
JS learning notes - first acquaintance
Comment réaliser un graphique Nebula d'importation CSV hors ligne de niveau milliard
Why does the system convert the temp environment variable to a short file name?
The outline dimension function application of small motherboard
Moveit 避障路径规划 demo
Solve the problem of base64encoder error
2020.4.12 byte written test questions B DP D monotone stack
Compress words (kmp/ string hash, double hash)
手机app通达信添加自定义公式(分时T+0)为例子讲解
Maui学习之路(三)--Winui3深入探讨
愛可可AI前沿推介(7.2)
Astra: could not open "2bc5/ [email protected] /6“: Failed to set USB interface
Boot 中bean配置覆盖
Locate: cannot execute stat() `/var/lib/mlocate/mlocate Db ': there is no such file or directory
Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network
Multi data source configuration code
《大学“电路分析基础”课程实验合集.实验七》丨正弦稳态电路的研究