【LeetCode】16. 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).
思路:和上一题差不多,将三个数的和减去target取绝对值,每次找出误差最小值及对应的三个数的和。
public class Solution {
public int threeSumClosest(int[] nums, int target) {
int len=nums.length;
int closestVal=Integer.MAX_VALUE;
int closestSum=nums[0]+nums[1]+nums[2];
Arrays.sort(nums);
int currentNum=nums[0];
for(int i=0;i<len-2;i++){
if(i>0 && nums[i]==currentNum) continue;
int begin=i+1,end=len-1;
while(begin<end){
int sum=nums[i]+nums[begin]+nums[end];
int absVal=Math.abs(sum-target);
if(absVal<closestVal){
closestVal=absVal;
closestSum=sum;
}else if(sum<target){
begin++;
}else end--;
}
currentNum=nums[i];
}
return closestSum;
}
}
【LeetCode】16. 3Sum Closest的更多相关文章
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【一天一道LeetCode】#16. 3Sum Closest
一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...
- 【LeetCode】016 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- Leetcode Array 16 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 【LeetCode】973. K Closest Points to Origin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...
随机推荐
- IIS SMTP status codes
Here are the meaning of SMTP status codes. Status Code Description 211 System status, or system help ...
- 黄聪:C#如何通过MeasureString、Graphics获取字符串的像素长度
1. 使用g.MeasureString()获得 使用MeasureString测量出来的字符宽度,总是比实际宽度大一些,而且随着字符的长度增大,貌似实际宽度和测量宽度的差距也越来越大了.查了一 ...
- Callable和Future
在并发编程时,一般使用runnable,然后扔给线程池完事,这种情况下不需要线程的结果. 所以run的返回值是void类型. 如果是一个多线程协作程序,比如菲波拉切数列,1,1,2,3,5,8...使 ...
- (WPF) 文件和文件夹选择对话框。
点击button,选择一个excel文件,并将文件名显示在textbox上. private void btnSelectErrorTableFile_Click(object sender, Rou ...
- 不用配置tnsnames.ora,直接通过PL/SQL访问远程数据库
- [CSS]图片与文字对齐问题
摘自:张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com] 图片与文字默认是居底对齐了.所以当图片与文字在一起的时候往往都是不对齐的.尤其图片较小时就更加明显了,我看到很多人使 ...
- PLSQL_性能优化工具系列02_SQL Tuning Health-Check Script (SQLHC)
2014-08-23 Created By BaoXinjian
- 为什么学习html5
html5 2010年正式推出 新的web时代 优势: 1,跨平台运行 PC MAC LINUX 手机 PAD 2,硬件要求低 3,flash之外的选择 h ...
- ICANN认证企业列表
查询地址: http://www.internic.net/alpha.html
- sql工作问题总结
1. sql排序:1. order by ……2. row_number() over(partition by …… order by ……) 使用说明:此函数适合做分组.排序,而不能在使用它分组的 ...