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 ...
随机推荐
- Python学习笔记-字典
字典是python中唯一内建的映射类型. 创建字典phonebook = {'Alice':'2341','Beth':'9102'} 可以使用dict通过其他映射或者键值对的序列建立字典.关键值参数 ...
- c# List 按类的指定字段排序
List<Report> list=Report.FindAllbyClassID(id); ) { list.Sort(delegate(Report x,Report y){ret ...
- Python控制语句
1.结构化程序设计 结构化程序设计包含三种基本结构:顺序结构.选择结构.循环结构. 1)顺序结构 如图所示,虚线框内是一个顺序结构:其中A和B两个框是顺序执行的,即在执行完A框所指定的操作后,必然接着 ...
- iOS类实现里面怎么用属性
属性(properity)是一个很好用的东西,简单而直接.Objective-C还创建了一个点语法来帮助大家使用.根据以前C++的习惯,类外访问实例变量时,最好通过getter/setter方法,也就 ...
- unix exec族函数 关于参数的疑惑
问题不出在这几个函数,而在于看后文解释器的时候发现一个很奇妙的问题. #include <unistd.h> int execl(const char *pathname, const c ...
- JavaScript Emoji 表情库_js 类似于qq微信的表情库
摘要: emoji就是表情符号,来自日语词汇“絵文字”(假名为“えもじ”,读音即emoji).emoji的创造者是日本人栗田穰崇(Shigetaka Kurita),他将目光投向儿时的各种元素以获取灵 ...
- Opencv创建有滚动条的视频
#include "stdafx.h"#include "cv.h"#include "cxcore.h"#include "hi ...
- Json.net 忽略实体某些属性的序列化
遇到了一个小问题有一个用户类,结构和数据库一模一样,里面包含用户密码,要向前台返回用户信息的json数据,但是不能输出密码这个字段.之前的做法是 重新又建了一个不包含这个字段的新类,然后深度复制,总感 ...
- Error:(1, 0) Your project path contains non-ASCII characters. This will most likely cause the build to fail on Windows. Please move your project to a different directory. See http://b.android.com/9574
这个一般是因为你打开的项目所在的目录存在中文,把项目换到全英文目录下就没问题了
- dedecms recommend 注入 exp
我看没人用python写过发过 所以我就发一下 喜欢用python的就用我这个吧 不喜欢的就用JAR那个或者PHP那个吧 #coding:GBK import re import urllib &q ...