LeetCode-016-最接近的三数之和
最接近的三数之和
题目描述:给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。
示例说明请见LeetCode官网。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/3sum-closest/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法一:双指针法
首先将数组排序,获取一个初始的结果difference,然后第一个数字first从第一个数字开始遍历,第二个数字second和第三个数字third分别从之后的数组中从两边向中间遍历,遍历过程中如果与target比difference更接近,则更新difference,直到遍历完成,返回difference对应的result。
说明:解法和 LeetCode-015-三数之和 这个题目的解法类似,可对比参考。
import java.util.Arrays;
public class Solution {
/**
* 双指针法
* @param nums
* @param target
* @return
*/
public static int threeSumClosest(int[] nums, int target) {
if (nums.length == 3) {
return nums[0] + nums[1] + nums[2];
}
Arrays.sort(nums);
int result = nums[0] + nums[1] + nums[2];
int difference = Math.abs(result - target);
// 第一个数字
for (int first = 0; first < nums.length - 2; first++) {
//过滤掉重复的
if (first > 0 && nums[first] == nums[first - 1]) {
continue;
}
for (int second = first + 1; second < nums.length - 1; second++) {
if (second > first + 1 && nums[second] == nums[second - 1]) {
// 过滤掉重复的
continue;
}
int third = nums.length - 1;
while (second < third) {
// 当3个数之和比当前的result更接近时,更新result
if (Math.abs(nums[first] + nums[second] + nums[third] - target) < difference) {
result = nums[first] + nums[second] + nums[third];
difference = Math.abs(result - target);
}
third--;
}
}
}
return result;
}
public static void main(String[] args) {
int[] nums = new int[]{0, 1, 2, 3, 4, 5, 6};
System.out.println(threeSumClosest(nums, 2));
}
}
【每日寄语】健康自己就是最大的关爱别人。
LeetCode-016-最接近的三数之和的更多相关文章
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- LeetCode 16. 最接近的三数之和(3Sum Closest)
题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例 ...
- 【LeetCode】最接近的三数之和【排序,固定k1,二分寻找k2和k3】
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- [LeetCode] 16. 最接近的三数之和
题目链接:https://leetcode-cn.com/problems/3sum-closest/ 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 num ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode题库——16.最接近的三数之和
@author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...
- lintcode-59-最接近的三数之和
59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...
- Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 定义一个类继承BroadCastReceiver public class OutGoingCallReceiver extends BroadcastReceiver { //当接收到外拨电话 ...
- 动态修改UINavigationBar的背景色--by-胡旭
这是我们最终想要得到的效果 思路 在UISrollView的delegate方法 - (void)scrollViewDidScroll:(UIScrollView *)scrollView中根据当前 ...
- 直播媒体流red5
第一步下载 安装setup-Red5-1.0.1-java6.exe 安装教程网上有很多 显示如下页面说明安装成功 第二步 下载oflaDemo的 解压放在 第三步 打开 然后 ok了 注意:下面的 ...
- Apache——配置与应用
Apache配置与应用 1.概述 2.httpd服务支持的虚拟主机类型 3.构建虚拟Web主机 4.构建Web虚拟目录与用户授权限制 5.日志分割 6.AWStats分析系统 1.概述: 虚拟web主 ...
- SpringBoot源码解读系列——开篇
什么是SpringBoot? 定义可以参考官网:SpringBoot官网,其定义通俗易懂,这里就不赘述. 官网也给出了一个通用的SpringBoot工程样例,其中包含了这么几个元素: 1.pom依赖 ...
- HTTPStatus(状态码返回)详情
1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 100 (继续) 请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分,正在等待其余部分. 101 (切 ...