题面

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

给定数组,找出并返回最接近target的三个元素的和。可以假设,只有一个解。

样例

Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

思路

我们在15题 3Sum中做过,三数加和为target的问题,采用了Two-Point逼近的方法。本题我们稍加改动就可以解决。

1. 数组排序,固定一个元素i,通过两点法去[i+1, size()-1]中搜索另外两个数字,先计算他们的和;

2. 若sum == target,直接返回;若不等,就需要判断sum与 我们给的初值res 那个更加接近target,即判断 abs(sum - target)  与 abs(res - target)的大小,对res进行更新,另外注意 l 和 r 的更新。

3. 返回res.

源码

 class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int len = nums.size();
if(len < )
return ;
//数组升序排序
sort(nums.begin(), nums.end());
int res = nums[]+nums[]+nums[];
for(int i=; i<len; i++)
{
if(i> && nums[i]==nums[i-])
i++;//避免i的重复,不必要
int l = i+, r = len-;
while(l < r)//两点法搜搜
{
int sum = nums[i] + nums[l] + nums[r];
if(sum == target)
return sum;
else if(sum > target)
{
if(abs(sum - target) < abs(res - target))
res = sum;
r--;
}
else
{
if(abs(sum - target) < abs(res - target))
res = sum;
l++;
}
}
}
return res;
}
};

Array + two points leetcode.16 - 3Sum Closest的更多相关文章

  1. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  2. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  3. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  4. Leetcode 16. 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  5. Java [leetcode 16] 3Sum Closest

    题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...

  6. [LeetCode] 16. 3Sum Closest 解题思路

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. LeetCode 16. 3Sum Closest. (最接近的三数之和)

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  8. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

    题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  9. [LeetCode] 16. 3Sum Closest ☆☆☆

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

随机推荐

  1. scala的wordcount

    import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.sql.SparkSession object Wo ...

  2. 使用ASP.NET Core支持GraphQL( restful 配套)

    https://github.com/graphql-dotnet https://github.com/graphql GraphQL简介 官网:https://graphql.cn/code/ 下 ...

  3. 到处抄来的SUCTF2019 web wp

    0x01 EasySQL 这是一个考察堆叠注入的题目,但是这道题因为作者的过滤不够完全所以存在非预期解 非预期解 直接构造 *,1 这样构造,最后拼接的查询语句就变成了 select *,1||fla ...

  4. Spring 多对对实体

    package com.wangshenghua.entity; import java.io.Serializable; import java.util.Set; import javax.per ...

  5. Windows 10系统快捷键

    虚拟桌面 创建新的虚拟桌面:Win + Ctrl + D 关闭当前虚拟桌面:Win + Ctrl + F4 切换虚拟桌面:Win + Ctrl +左/右 任务视图:Win + Tab Win10常用W ...

  6. CentOS使用yum安装jdk

    1.查看系统版本命令 cat /etc/issue 2.查看yum包含的jdk版本 yum search java 或者 yum list java* 版本 jre jdk 1.8 java-1.8. ...

  7. C语言位操作中指定的某一位数置0、置1、取反

    一.指定的某一位数置1 宏 #define setbit(x,y)  x|=(1<<y) 二.指定的某一位数置0 宏  #define clrbit(x,y)  x&=~(1< ...

  8. ubuntu18和windows10双系统时间不同步问题(Ubuntu)

    1.安装并校准时间 sudo apt install ntpdate sudo ntpdate time.windows.com 2.写入硬件配置 sudo hwclock --localtime - ...

  9. python学习-27 匿名函数

    匿名函数 1. 语法:   lanbda x:x+1 def a(x): return x+1 res = a(10) print(res) 运行结果: 11 Process finished wit ...

  10. C++ 中不能声明为虚函数的函数有哪些?

    目录 普通函数 构造函数 内联成员函数 静态成员函数 友元函数 普通函数 普通函数(非成员函数)只能被overload,不能被override,而且编译器会在编译时绑定函数. 多态的运行期行为体现在虚 ...