Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

详见:https://leetcode.com/problems/3sum-closest/description/

实现语言:Java

class Solution {
public int threeSumClosest(int[] nums, int target) {
int size=nums.length;
if(size<3||nums==null){
return 0;
}
int closest=nums[0]+nums[1]+nums[2];
int diff=Math.abs(closest-target);
Arrays.sort(nums);
for(int i=0;i<size-2;++i){
int j=i+1;
int k=size-1;
while(j<k){
int sum=nums[i]+nums[j]+nums[k];
int newDiff=Math.abs(sum-target);
if(diff>newDiff){
diff=newDiff;
closest=sum;
}
if(sum>target){
--k;
}else{
++j;
}
}
}
return closest;
}
}

实现语言:C++

class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int size=nums.size();
if(nums.empty()||size<3)
{
return 0;
}
int closest=nums[0]+nums[1]+nums[2];
int diff=abs(closest-target);
sort(nums.begin(),nums.end());
for(int i=0;i<size-2;++i)
{
int j=i+1;
int k=size-1;
while(j<k)
{
int sum=nums[i]+nums[j]+nums[k];
int newDiff=abs(sum-target);
if(diff>newDiff)
{
diff=newDiff;
closest=sum;
}
if(sum>target)
{
--k;
}
else
{
++j;
}
}
}
return closest;
}
};

016 3Sum Closest 最接近的三数之和的更多相关文章

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

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

  2. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  3. Leetcode16.3Sum Closest最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  4. [leetcode]16. 3Sum Closest最接近的三数之和

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

  5. lintcode-59-最接近的三数之和

    59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...

  6. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

  7. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  8. Java实现 LeetCode 16 最接近的三数之和

    16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...

  9. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

随机推荐

  1. WPF Canvas

    Canvas为容器控件,用于定位. 1.基本应用 <Border HorizontalAlignment="Left" VerticalAlignment="Top ...

  2. K.I.S.S 原则

    KEEP IT SIMPLE, STUPID! 编写只做一件事情,并且要做好的程序:编写可以在一起工作的程序,编写处理文本流的程序,因为这是通用的接口.这就是UNIX哲学.所有的哲学真 正的浓缩为一个 ...

  3. Linux内核解析

     一.Linux内核  一个完整可用的操作系统主要由 4 部分组成:硬件.操作系统内核.操作系统服务和用户应用程序,如下图所示:             用户应用程序:是指那些自处理程序. Inter ...

  4. ServerSocket01

    ServerSocket表示服务端套接字:我们首先来看下其中的一个构造器: public ServerSocket(int port,int backlog) throws IOException 其 ...

  5. [poj1273]Drainage Ditches(最大流)

    解题关键:最大流裸题 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

  6. 阶段2-新手上路\项目-移动物体监控系统\Sprint4-嵌入式web服务器开发\第3课-CGI程序开发

    实现CGI程序显示一幅图片最核心的功能 把上一节课编写好的led.c程序拷贝过来,并重新命名为image.c 把led的某些部分删除,后如下 那么如何显示一幅图片呢,百度(搜索在html里面去插入图片 ...

  7. Robots.txt在项目中的运用

    在开发公司一个项目的过程中,有这样一个需求 该网站上面有一个search功能,可以search该网站上的任何包括特定内容的网页 现在有一个需求,就是针对几个特定的页面,我们希望网站上的search功能 ...

  8. ASP.NET MVC中的ActionFilter介绍学习

    一直都知道MVC中的ActionFilter,只是没有在实际项目中使用过. 顾名思义,ActionFilter就是指在Action上的Filter, 那么,在Action上的Filter到底有哪些呢. ...

  9. ProtoBuf练习(三)

    任意类型 protobuf语言的任意字段类型相当于Boost库的boost::any类型数据,google.protobuf.Any是对protobuf语言的message进行封装,所以需要使用mes ...

  10. [WIP]Vue 基础

    创建: 2019/05/30 https://cn.vuejs.org/v2/guide/  安装 初期选择直接嵌入 <!-- 开发环境版本,包含了有帮助的命令行警告 --> <sc ...