016 3Sum Closest 最接近的三数之和
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 最接近的三数之和的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- Leetcode16.3Sum Closest最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- [leetcode]16. 3Sum Closest最接近的三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- lintcode-59-最接近的三数之和
59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...
- Leetcode题库——16.最接近的三数之和
@author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数  ...
- ios判断是否为iphone6或iphone6plus代码
转自:http://blog.csdn.net/lvxiangan/article/details/45288505 #define IS_IPAD (UI_USER_INTERFACE_IDIOM( ...
- Mac搭建nginx+rtmp服务器
nginx是非常优秀的开源服务器,用它来做hls或者rtmp流媒体服务器是非常不错的选择,本人在网上整理了安装流程,分享给大家并且作备忘. 一.安装Homebrow 已经安装了brow的可以直接跳过这 ...
- C#设计模式(11)——外观模式
一.概念 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用.使用外观模式时,我们创建了一个统一的类,用来包装子系统中一个或多个复杂的类,客户端可以直 ...
- Ubuntu 安装配置Jenkins
一.安装jdk 1. sudo add-apt-repository ppa:webupd8team/java 添加ppa源 如果提示找不到该命令则需要安装: sudo apt-get install ...
- Linux性能测试工具-UnixBench--安装以及结果分析
参考链接:http://blog.csdn.net/gatieme/article/details/50912910 依赖包安装: yum install -y SDL-devel mesa ...
- 转——AHRS(航姿参考系统)和IMU(惯性测量单元)的区别
AHRS(航姿参考系统)和IMU(惯性测量单元)的区别 [测试测量] 发布时间:2010-05-09 16:52:09 http://bbs.ednchina.com/BLOG_ARTICLE_1 ...
- shell工具使用配置备忘
一.bash之vi mode.两种方式:set -o vi(只让bash自己进入vi模式)或 set editing-mode vi(让所有使用readline库函数的程序在读取命令行时都进入vi模式 ...
- Linux/Unix 指令使用说明的格式介绍(the Bash Command 'Usage' Syntax)
Linux/Unix 指令使用说明的格式介绍(the Bash Command 'Usage' Syntax) 摘自 金马的Blog 原文 http://www.lijinma.com/blo ...