3Sum Closest leetcode java
题目:
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).
题解:
这道题也是3sum的变体,这里找到的不仅使3sum==target,同时如果没有找到==target的3sum要返回最接近target的值。
于是,这就需要在使用二分查找法时遍历数组的时候,维护一个最接近target值min,这样当完全遍历完数组扔没找到与target相等的3sum时,可以返回维护的这个min值。
这道题比3sum和4sum简单的地方就是不需要判断重复问题,因为题目给我们减轻了去重的压力,have exactly one solution。
即便要求去重,使用之前说过的两个方法:HashSet和挪动指针法,也可以很容易就去重了。
这里要注意,判断closest的方法是采取target-sum的绝对值与min相比,很容易理解,无论这个closest是在target左还是右,离target最近的就是最closest的。 实现代码如下:
1 public int threeSumClosest(int[] num, int target) {
2 if(num==null || num.length<3)
3 return 0;
4
5 int min = Integer.MAX_VALUE;
6 int val = 0;
7 Arrays.sort(num);
8 for(int i = 0; i<=num.length-3;i++){
9 int low = i+1;
int high = num.length-1;
while(low<high){
int sum = num[i]+num[low]+num[high];
if(Math.abs(target-sum)<min){
min = Math.abs(target-sum);
val = sum;
}
if(target==sum){
return val;
}else if(target>sum){
low++;
}else{
high--;
}
}
}
return val;
}
3Sum Closest leetcode java的更多相关文章
- 3Sum Closest - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...
- 3Sum Closest——LeetCode
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
随机推荐
- react篇章-React Props
state 和 props 主要的区别在于 props 是不可变的,而 state 可以根据与用户交互来改变.这就是为什么有些容器组件需要定义 state 来更新和修改数据. 而子组件只能通过 pro ...
- vue.js 是如何做到数据响应的
许多前端JavaScript框架(例如Angular,React和Vue)都有自己的数据相应引擎.通过了解相应性及其工作原理,您可以提高开发技能并更有效地使用JavaScript框架.在视频和下面的文 ...
- Web服务评估工具Nikto
Web服务评估工具Nikto Nikto是一款Perl语言编写的Web服务评估工具.该工具主要侧重于发现网站的默认配置和错误配置.它通过扫描的方式,检测服务器.服务和网站的配置文件,从中找出默认配 ...
- Django快速创建博客,包含了整个框架使用过程,简单易懂
创建工程 ...
- 深入理解ajax系列第六篇
前面的话 每个HTTP请求和响应都会带有相应的头部信息,其中有的对开发人员有用.XHR对象提供了操作头部信息的方法.本文将详细介绍HTTP的头部信息 默认信息 默认情况下,在发送XHR请求的同时,还会 ...
- BZOJ2055 80人环游世界 网络流 费用流 有源汇有上下界的费用流
https://darkbzoj.cf/problem/2055 https://blog.csdn.net/Clove_unique/article/details/54864211 ←对有上下界费 ...
- 【20181103T1】地球发动机【dp优化】
题面 一眼dp 设\(f_i\)表示前\(i\)个且\(i\)必须选的最大功率 有 \(f _i= max_{1 \leq j < i,A_i - A_j > X_j} \{f_j \}+ ...
- 【单调队列】BZOJ1342-[Baltic2007]Sound静音问题
[题目大意] 给出一个n个数的序列,以哪位位置为开头的长度为m的区间满足该区间的最大值与最小值的差≤一个定值. [思路] 单调队列……说一下单调队列比较方便的操作. 把第一个先丢进去,开始条件为hea ...
- VC++ 设置控件显示文本的前景色、背景色以及字体
在每个控件开始绘制之前,都会向其父窗口发送WM_CTLCOLOR通告消息,在该消息的处理函数中,可以设置控件显示文本的前景色.背景色以及字体.该消息处理函数还要求返回一个画刷的句柄,用于在控件具体的绘 ...
- Slickflow.NET 开源工作流引擎基础介绍(二) -- 引擎组件和业务系统的集成
集成流程引擎的必要性 业务过程的变化是在BPM系统中常见的现象,企业管理层需要不断优化组织架构,改造业务流程,不可避免地带来了业务流程的变化,企业信息系统就会随之面临重构的可能性.一种直接的方式是改造 ...