当前位置:网站首页>(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;
}
};
边栏推荐
- [idea] recommend an idea translation plug-in: translation "suggestions collection"
- Nebula Graph & 数仓血缘关系数据的存储与读写
- AWS virtual machine expansion
- C # get PLC information (kepserver) II
- JS learning notes - data types
- Postgressql stream replication active / standby switchover primary database no read / write downtime scenario
- 数字藏品系统开发(程序开发)丨数字藏品3D建模经济模式系统开发源码
- Ssh/scp does not prompt all activities are monitored and reported
- /Bin/ld: cannot find -lpam
- mysql 计算经纬度范围内的数据
猜你喜欢
Application of visualization technology in Nebula graph
Xpt2046 four wire resistive touch screen
win10系统升级一段时间后,内存占用过高
华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys
After the win10 system is upgraded for a period of time, the memory occupation is too high
Teach you how to build virtual machines locally and deploy microservices
Experiment collection of University "Fundamentals of circuit analysis". Experiment 7 - Research on sinusoidal steady-state circuit
Armv8-a programming guide MMU (4)
图数据库|Nebula Graph v3.1.0 性能报告
Introduction to dynamic planning I, BFS of queue (70.121.279.200)
随机推荐
/Bin/ld: cannot find -lgssapi_ krb5
HMS core machine learning service helps zaful users to shop conveniently
智联招聘的基于 Nebula Graph 的推荐实践分享
Fiddler realizes mobile packet capturing - getting started
Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network
C # get PLC information (kepserver) II
可视化技术在 Nebula Graph 中的应用
图数据库|Nebula Graph v3.1.0 性能报告
Best practices for building multi architecture images
Postgressql stream replication active / standby switchover primary database no read / write downtime scenario
Boot connection to impala database
Nebula Graph & 数仓血缘关系数据的存储与读写
SQLServer查询哪些索引利用率低
Application of visualization technology in Nebula graph
《大学“电路分析基础”课程实验合集.实验六》丨典型信号的观察与测量
Invalid bound statement (not found)解决方法总结
Aike AI frontier promotion (7.2)
Boot 事务使用
[5g NR] RRC connection release
How to import a billion level offline CSV into Nepal graph