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 最基础生命周期及旋转屏幕问题
public class MainActivity extends Activity { private static final String TAG ="MainActivity&quo ...
- Java_常用API
1.基本类型包装类 1.1 概述 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类型 ...
- 【推导】【贪心】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) D. Riverside Curio
题意:海平面每天高度会变化,一个人会在每天海平面的位置刻下一道痕迹(如果当前位置没有已经刻划过的痕迹),并且记录下当天比海平面高的痕迹有多少条,记为a[i].让你最小化每天比海平面低的痕迹条数之和. ...
- 【dijkstra优化/次短路径】POJ3255-Roadblocks
[题目大意] 给出一张无向图,求出从源点到终点的次短边. [思路] 先来谈谈Dijkstra的优化.对于每次寻找到当前为访问过的点中距离最短的那一个,运用优先队列进行优化,避免全部扫描,每更新一个点的 ...
- 2018-2019-20172329 《Java软件结构与数据结构》第三周学习总结
2018-2019-20172329 <Java软件结构与数据结构>第三周学习总结 教材学习内容总结 <Java软件结构与数据结构>第五章-队列 一.概述 1.队列是什么? 队 ...
- python及其模块下载集合
1)python平台 https://www.python.org/downloads/ 2)打包工具 cx-freeze(python3以上版本打包工具) http://cx-freeze.sour ...
- opencv第一课 打开一个图片
#include<stdio.h>#include<stdlib.h>#include<opencv2\opencv.hpp>#include<iostrea ...
- ROW_NUMBER() OVER函数运用
语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) PARTITION BY:相当于数据库中的group by 说明:row_numbe ...
- Python— isinstance用法说明
在学习自动化测试的脚本中发现了这个函数,所以在网上查了一下资料进行如下整理: 通过帮助查看如下: 作用:来判断一个对象是否是一个已知的类型: 其第一个参数(object)为对象,第二个参数为类型名(i ...
- redux基础(1)
redux ps:每个案例都是接着上一个案例写的 主要以案例讲解如何使用,具体概念请参考如下: 基本概念参考1 基本概念参考2 案例源码戳这里 一.Store.Action.Reducer简介 Sto ...