当前位置:网站首页>(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;
}
};边栏推荐
- 多数据源配置代码
- mysql 计算经纬度范围内的数据
- 又是一年毕业季
- [5g NR] RRC connection release
- Some problems about pytorch extension
- [idea] recommend an idea translation plug-in: translation "suggestions collection"
- 华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys
- By asp Net core downloads files according to the path exception
- After the win10 system is upgraded for a period of time, the memory occupation is too high
- Make p12 certificate [easy to understand]
猜你喜欢

去除router-link中的下划线

Nebula Graph & 数仓血缘关系数据的存储与读写

如何实现十亿级离线 CSV 导入 Nebula Graph

又是一年毕业季

通过两级网关设计来路由服务网格流量

Boot 事务使用

《大学“电路分析基础”课程实验合集.实验四》丨线性电路特性的研究

Storage, reading and writing of blood relationship data of Nepal Graph & Data Warehouse

Pattern matching extraction of specific subgraphs in graphx graph Computing Practice

Boot transaction usage
随机推荐
《大学“电路分析基础”课程实验合集.实验六》丨典型信号的观察与测量
Boot 事务使用
Idea public method extraction shortcut key
2020.4.12 byte written test questions B DP D monotone stack
C # get PLC information (kepserver) II
Nebula Graph & 数仓血缘关系数据的存储与读写
愛可可AI前沿推介(7.2)
The sea of stars hidden behind the nebula graph
The outline dimension function application of small motherboard
原神2.6服务端下载以及搭建安装教程
Comment réaliser un graphique Nebula d'importation CSV hors ligne de niveau milliard
/Bin/ld: cannot find -lpam
如何實現十億級離線 CSV 導入 Nebula Graph
Introduction to dynamic planning I, BFS of queue (70.121.279.200)
XPT2046 四线电阻式触摸屏
多数据源配置代码
How to import a billion level offline CSV into Nepal graph
Digital collection system development (program development) - Digital Collection 3D modeling economic model system development source code
Leetcode -- number of palindromes
[idea] recommend an idea translation plug-in: translation "suggestions collection"