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. 电话号码的字母组合
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ,则 \(x\) 称为 \(a\bmod p\) 的逆元,记作 \(a^{-1}\). ...
- 关于obj.class.getResource()和obj.getClass().getClassLoader().getResource()的路径问题
感谢原文作者:yejg1212 原文链接:https://www.cnblogs.com/yejg1212/p/3270152.html 注:格式内容与原文有轻微不同. Java中取资源时,经常用到C ...
- 关于在findViewById()方法时遇到的一些问题
最近需要做一个关于色卡的App,需要用到LayoutInflater来实现标签动态切换View界面,但是发现在使用过程中App突然会闪退, 闪退目前已解决. 先看下关于findViewById()的详 ...
- xargs、sort、uniq命令
xargs.sort.uniq命令,我们由LeetCode的一道题来引入,并使用加以理解: 题目是这样的:写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率. word ...
- 【HDU6647】Bracket Sequences on Tree(树Hash 树上Dp)
题目链接 大意 给出一颗树,按下列方式生成一个括号序列. function dfs(int cur, int parent): print('(') for all nxt that cur is a ...
- postman常用测试脚本
测试脚本: 设置环境变量 var jsonData = JSON.parse(responseBody); postman.setGlobalVariable("5KMST", j ...
- Vue 子组件更新父组件的值
今天在使用Vue中遇到了一个新的需求:子组件需要修改由父组件传递过来的值,由于子组件的值是由父组件传递过来的,不能直接修改属性的值, 我们想改变传递过来的值只能通过自定义事件的形式修改父组件的值达到修 ...
- Solution -「NOI.AC 省选膜你赛」array
题目 题意简述 维护一个长度为 \(n\) 的序列 \(\{a_n\}\),并给出 \(q\) 个操作: 将下标为 \(x\) 的数修改为 \(y\). 给定 \(l,r,k\),求最大的 \(m ...