No.016 3Sum Closest
16. 3Sum Closest
- Total Accepted: 86565
- Total Submissions: 291260
- Difficulty: Medium
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的解法有点类似,我们可以先将数组排序,然后将数组中的数从左到右依次确定为第一个数,在其右边的数中寻找最接近的target的数即可。重要的是判断逻辑的思路。代码如下:
public int threeSumClosest(int[] nums, int target) {
if (nums == null || nums.length < 3) {
return 0;
}
Arrays.sort(nums);
int closest = nums[0]+nums[1]+nums[2] ;
for(int i = 0 ; i < nums.length-2 ; i++){
int l = i+1 ;
int r = nums.length-1 ;
while(l < r){
int sum = nums[i] + nums[l] + nums[r] ;
if(sum == target){
return sum ;
}else if(sum < target){
/*
* 三种情况:
* 1、closest < sum < target --->closest = sum
* 2、sum < target < closest --->| target-sum < closest-target ---> closest = sum
* | target-sum >= closest-target --> closest不变
* 3、sum <= closest <= target ---> closest不变
*/
if(sum > closest){
//情况1
closest = sum ;
}else if(closest > target){
//情况2
if(target-sum < closest-target){
//情况2.1,
closest = sum ;
}
//情况2.2不用变化
}
//情况3不同变化
l++ ;
}else{
//分析同上
if(sum < closest){
closest = sum ;
}else if(closest < target){
if(sum-target <= target-closest){
closest = sum ;
}
}
r-- ;
}
}
}
return closest ;
}
No.016 3Sum Closest的更多相关文章
- LeetCode--No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- 【JAVA、C++】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 num ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- 016 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】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]016. 3Sum Closest
public class Solution { public int threeSumClosest(int[] num, int target) { int result = num[0] + nu ...
- 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 ...
- 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
随机推荐
- MYSQL数据表操作语句
1.查看某数据库中的表 SHOW [FULL] TABLES [FROM db_name] [LIKE 'pattern'] SHOW TABLES列举了给定数据库中的非TEMPORARY表.也可以使 ...
- “cv”: 具有该名称的命名空间不存在
记得添加#include<highgui.h> 无法解析的外部符号 遇到这种问题一般都是由于缺少相应的库文件 右击项目,选择“属性”--“链接器”--“输入”--“附加依赖项” 根据错误中 ...
- 黑马程序员:Java编程_String
=========== ASP.Net+Android+IOS开发..Net培训.期待与您交流!=========== 描述字符串对象的类是java.lang.String,String类是不可变(f ...
- PAT (Basic Level) Practise:1032. 挖掘机技术哪家强
[题目链接] 为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行 ...
- Linux字符界面的优势
优势一:字符界面占用的系统资源更少,更加适合服务器 优势二:字符界面减少了出错.被攻击的可能性(一.二决定了服务器一般不装图形界面,安全稳定优先)
- 《C与指针》第九章练习
本章问题 1.C语言缺少显示的字符串数据类型,这是一个优点还是一个缺点? answer: (这个问题存在争论(尽管我有一个结论))目前这个方法的优点是字符数组的效率和访问的灵活性,它的缺点是有可能引起 ...
- C#常见控件命名规则举例
控件 缩写 举例 Adrotator adrt adrtTopAd BulletedList blst blstCity Button btn btnSubmit Calendar ca ...
- TeleportStone.lua --传送宝石
--[[作者信息: 超级炉石 (Teleport stone) 作者QQ:247321453 作者Email:247321453@qq.com 修改日期:2014-3-12 功能:除了传送,还有召唤N ...
- JdbcUtils 系列1
1.开发前准备 创建java pro为dbutils_1,没有lib目录,建一个即可 /dbutils_1/lib/mysql-connector-java-5.0.8-bin.jar 数据库搭建c3 ...
- RequestContextListener作用
spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype ...