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. 电话号码的字母组合
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 
from collections imort OrderedDict d = OrderedDict() d['aa'] = (1, 30) d['bb'] = (2, 31) d['cc'] = ( ...
- MongoDB之二基础入门(window/linux安装启动)
mongodb中有三元素:数据库,集合,文档,其中“集合”就是对应关系数据库中的“表”,“文档”对应“行”. 一window安装与启动 一. 下载 上MongoDB官网 ,下载页面:https://w ...
- Python函数式编程(把函数作为参数传入)
map:接受两个参数(函数,Iterable),map将传入的函数依次作用于Iterable的每个元素,并且返回新的Iterable def f(x): return x*x r = map(f,[1 ...
- 6.JasperReports学习笔记6-jasperreports和ssh工程整合
转自:http://www.blogjava.net/vjame/archive/2013/10/12/404908.html 一.导入jasperreport相关jar包,这里采用当前比较稳定的5. ...
- (Python编程)Pickle对象
Programming Python, 3rd Edition 翻译 最新版本见:http://wiki.woodpecker.org.cn/moin/PP3eD 19.4. Pickled Obje ...
- centos6.5安装zookeeper教程(三)
阅读前建议先阅读: http://www.cnblogs.com/duenboa/articles/6665159.html 1. 下载安装文件zookeeper-3.4.6.tar.gz 镜像地 ...
- 【jeasyui5】样式:调整页面显示的顶部菜单和左侧菜单
1.顶部菜单修改:修改index2.js里面的InitTopMenu方法,将icon +2 2.左侧菜单宽度调整: 修改index.html,加上width:170的定长 <!-- 左侧菜单 - ...
- 20. Linux提权:从入门到放弃
几点前提 已经拿到低权shell 被入侵的机器上面有nc,python,perl等linux非常常见的工具 有权限上传文件和下载文件 内核漏洞提权 提到脏牛,运维流下两行眼泪,我们留下两行鼻血.内核漏 ...
- DropDownList绑定数据库
this.DropDownList_设备列表.DataSource = dt_eq;//设置数据源 this.DropDownList_设备列表.DataTextField = "equip ...