【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 ...
随机推荐
- js获取网页的各种高度和宽度
document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度document.document ...
- makefile详解 嵌套执行make,定义命令包
嵌套执行make 在一些大的工程中,我们会把我们不同模块或是不同功能的源文件放在不同的目录中,我们可以在每个目录中都书写一个该目录的Makefile,这有利于让我们的Makefile变得更加地简洁,而 ...
- grep 与正则表达式
正则表达式只是字符串的一种描述,只有和支持正则表达式的工具相结合才能进行字符串处理.本文以grep为例来讲解正则表达式. grep命令 功能:输入文件的每一行中查找字符串. 基本用法: grep [- ...
- Apache日志不记录图片文件设置方法和来源日志的配置
Apache日志不记录图片文件设置方法 <FilesMatch "\.(ico|gif|jpg|swf)">SetEnv IMAG 1</FilesMatch&g ...
- 手把手教你玩转Git分布式版本控制系统!
目录 Git诞生历史 Git环境准备 Git安装部署 Git常用命令 Git基本操作 Git管理分支结构 Git管理标签 GitLab安装部署 GitHub托管服务 Git客户端工具 1 Git诞生历 ...
- Linux命令(15)查看系统版本信息
一,查看Linux内核版本命令(两种方法) 1.cat /proc/version [user@fgejjw7Z home]$ cat /proc/version Linux version -.el ...
- 区分listview的item和Button的点击事件
这两天修改领导通的ListView widget,在ListView中加入Button这类的有 “点击” 事件的widget,发现原来listview的itemclick居然失效了, 后来在网上查资料 ...
- highstock实现股票分时
highchart学习网站 www.highcharts.com http://www.hcharts.cn/docs/index.php http://www.hcharts.cn/api/high ...
- java多线程的使用2
1.join与interrupt的用法 class Sleeper extends Thread { private int duration; public Sleeper(String name, ...
- Java高级开发工程师