当前位置:网站首页>(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;
}
};边栏推荐
- Boot 事务使用
- Leetcode --- longest public prefix
- Practice of constructing ten billion relationship knowledge map based on Nebula graph
- Usage of group by
- 【题解】Educational Codeforces Round 82
- Armv8-a programming guide MMU (4)
- Wavedec2 in MATLAB, talk about the wavedec2 function [easy to understand]
- Moveit obstacle avoidance path planning demo
- 去除router-link中的下划线
- Idea public method extraction shortcut key
猜你喜欢

idea jar包冲突排查

Aike AI frontier promotion (7.2)

After the win10 system is upgraded for a period of time, the memory occupation is too high

Traversal before, during and after binary tree

Write your own CPU Chapter 11 - learning notes

隐藏在 Nebula Graph 背后的星辰大海

JS learning notes - variables

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

Boot transaction usage

Pattern matching extraction of specific subgraphs in graphx graph Computing Practice
随机推荐
Some problems about pytorch extension
In memory of becoming the first dayu200 tripartite demo contributor
华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys
Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC
解决BASE64Encoder报错的问题
SQL FOREIGN KEY
Target detection - make your own deep learning target detection data set with labelimg
Practice of constructing ten billion relationship knowledge map based on Nebula graph
JS learning notes - process control
将点云坐标转换成世界坐标的demo
愛可可AI前沿推介(7.2)
Wise target detection 23 - pytoch builds SSD target detection platform
Postgressql stream replication active / standby switchover primary database no read / write downtime scenario
AWS virtual machine expansion
SQLServer查询哪些索引利用率低
Boot 连接 Impala数据库
JS learning notes - first acquaintance
处理gzip: stdin: not in gzip formattar: Child returned status 1tar: Error is not recoverable: exitin
Comment réaliser un graphique Nebula d'importation CSV hors ligne de niveau milliard
Construction and business practice of Zhongke brain knowledge map platform