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. 电话号码的字母组合
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数  throws IOException 其 ...
- [poj1273]Drainage Ditches(最大流)
解题关键:最大流裸题 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...
- 阶段2-新手上路\项目-移动物体监控系统\Sprint4-嵌入式web服务器开发\第3课-CGI程序开发
实现CGI程序显示一幅图片最核心的功能 把上一节课编写好的led.c程序拷贝过来,并重新命名为image.c 把led的某些部分删除,后如下 那么如何显示一幅图片呢,百度(搜索在html里面去插入图片 ...
- Robots.txt在项目中的运用
在开发公司一个项目的过程中,有这样一个需求 该网站上面有一个search功能,可以search该网站上的任何包括特定内容的网页 现在有一个需求,就是针对几个特定的页面,我们希望网站上的search功能 ...
- ASP.NET MVC中的ActionFilter介绍学习
一直都知道MVC中的ActionFilter,只是没有在实际项目中使用过. 顾名思义,ActionFilter就是指在Action上的Filter, 那么,在Action上的Filter到底有哪些呢. ...
- ProtoBuf练习(三)
任意类型 protobuf语言的任意字段类型相当于Boost库的boost::any类型数据,google.protobuf.Any是对protobuf语言的message进行封装,所以需要使用mes ...
- [WIP]Vue 基础
创建: 2019/05/30 https://cn.vuejs.org/v2/guide/ 安装 初期选择直接嵌入 <!-- 开发环境版本,包含了有帮助的命令行警告 --> <sc ...