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 ...
随机推荐
- android activity 窗口 样式
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 将 活动 设置成 窗口样式, 可以设置 主题 为 对话框, 或者 半透明. 安卓:主题= ...
- Opencv学习笔记5:Opencv处理彩虹图、铜色图、灰度反转图
一.概述: 人类能够观察到的光的波长范围是有限的,并且人类视觉有一个特点,只能分辨出二十几种灰度,也就是说即使采集到的灰度图像分辨率超级高,有上百个灰度级,但是很遗憾,人们只能看出二十几个,也就是说信 ...
- 机器学习<1>:基础概念
本文是笔者学习李航老师的经典教材<统计学习方法>第一章的学习笔记,分享在此,作为机器学习系列的开篇文章,在本系列中,将会逐一总结介绍主要的机器学习算法的基本原理.基于Python的具体实现 ...
- [BZOJ5303][HAOI2018]反色游戏(Tarjan)
暴力做法是列异或方程组后高斯消元,答案为2^自由元个数,可以得60分.但这个算法已经到此为止了. 从图论的角度考虑这个问题,当原图是一棵树时,可以从叶子开始唯一确定每条边的选择情况,所以答案为1. 于 ...
- Codeforces Round #463
A - Palindromic Supersequence /* 题目大意:给出一个串,构造一个包含该串的回文串 */ #include <cstdio> #include <alg ...
- 【assembly】用汇编写的一个BMP图片读取器
;----------------------------- ;文件满足256色调的 ;----------------------------- Stack Segment ...
- 20162327WJH使用队列:模拟票务站台代码分析
20162327WJH使用队列:模拟票务站台代码分析 用链队实现队列的情况 1.用链表实现队列的代码 关键方法代码及补全代(LinkedOueue类) public void enqueue(T el ...
- CentOS---网络配置详解
一.配置文件详解 在RHEL或者CentOS等Redhat系的Linux系统里,跟网络有关的主要设置文件如下: /etc/host.conf 配置域名服务客户端的控制文件 /etc/h ...
- hdu 1514 记忆化搜索
题意是给4堆(堆的高度小于等于40)有颜色(颜色的种类小于等于20)的物品,你有一个篮子最多能装5件物品,每次从这4堆物品里面任取一件物品放进篮子里,但是取每堆物品时,必须先取上面的物品,才能取下面的 ...
- ACM需要掌握算法
数据结构 栈,队列,链表 哈希表,哈希数组 堆,优先队列 双端队列 可并堆 左偏堆 二叉查找树 Treap 伸展树 并查集 集合计数问题 二分图的识别 平衡二叉树 二叉排序树 线段树 一维线段树 二维 ...