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. 电话号码的字母组合
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 
转自:https://www.jianshu.com/p/967cb76cd5ca 作为监控系统的后起之秀,prometheus的安装可谓非常简单,不需要第三方的依赖(数据库.缓存.PHP之类的).下 ...
- mysql获取表中的字段名
转载请注明来源:https://www.cnblogs.com/hookjc/ SELECT COLUMN_NAME FROM 'information_schema'.'COLUMNS' where ...
- uniapp自定义顶部搜索框兼容微信小程序
zhuanzai: uniapp自定义顶部搜索框兼容微信小程序 自定义组件 navbarvue (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度 < ...
- [源码解析] NVIDIA HugeCTR,GPU 版本参数服务器 --(1)
[源码解析] NVIDIA HugeCTR,GPU版本参数服务器 --(1) 目录 [源码解析] NVIDIA HugeCTR,GPU版本参数服务器 --(1) 0x00 摘要 0x01 背景 1.1 ...
- PHP+mysql真题
PHP+mysql真题 来自<PHP程序员面试笔试宝典>,涵盖了近三年了各大型企业常考的PHP面试题,针对面试题提取出来各种面试知识也涵盖在了本书. [真题215] 按要求写出SQL实现. ...
- Spring Security配置个过滤器也这么卷
以前胖哥带大家用Spring Security过滤器实现了验证码认证,今天我们来改良一下验证码认证的配置方式,更符合Spring Security的设计风格,也更加内卷. CaptchaAuthent ...