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. 通过pip3安装ipython

    操作系统:Centos7.4:ipython可以用来测试python语句,故需要安装. 首先需要安装epelde的扩展库: easy_install是由PEAK(Python Enterprise A ...

  2. oracle--循环PL/SQL--demo1---

    --简单的条件判断if–then --编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%. create or replace procedure sp_pro6 ...

  3. paramiko使用1

  4. css菜鸟学习之block,inline和inline-block概念和区别

    block,inline和inline-block概念和区别   总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) ...

  5. WPF x:key和x:name用法

    WPF x:key和x:name用法 x:Key用在xaml Resources,ResourceDictionary需要key来访问 x:Name用在ResourceDictionary以外任何地方 ...

  6. jquery 键盘事件的使用方法详解

    转自:https://www.jb51.net/article/123579.htm jQuery处理键盘事件有三个函数,根据事件发生的顺序分别是: jquery 代码: 1.  keydown(); ...

  7. k8s 基础 问题

    vim /usr/lib/systemd/system/docker.service --insecure-registry registry.access.redhat.com \ ubelet.s ...

  8. 快速实现CentOS7安装python-pip

    1.首先检查linux有没有安装python-pip包,终端执行 pip -V [root@ network-scripts]# pip -V -bash: pip: command not foun ...

  9. elasticsearch2.x插件之一:kibana

    介绍: 要说kibana,就不得不先说一下logstash.这里呢,先要讲个故事.故事是开头是这样的,Logstash早期曾经自带了一个特别简单的logstash-web用来查看ES中的数据,其功能太 ...

  10. github 分支操作

    1.查看分支 1.查看本地分支 使用git branch命令,如下: $ git branch * master *标识的是你当前所在的分支. 2.查看远程分支 命令如下: git branch -r ...