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. 电话号码的字母组合
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 黑盒测试:不需要写代码,给输入值,看程序能否给出期望值白盒测试:需要写代码,关注程序的具体执行流程junit使用步骤:步骤1:定义一个测试类建议类名,被测试类名后面加一个 ...
- 关于obj.class.getResource()和obj.getClass().getClassLoader().getResource()的路径问题
感谢原文作者:yejg1212 原文链接:https://www.cnblogs.com/yejg1212/p/3270152.html 注:格式内容与原文有轻微不同. Java中取资源时,经常用到C ...
- 匿名内部类不能访问外部类方法中的局部变量,除非变量被声明为final类型
1. 这里所说的"匿名内部类"主要是指在其外部类的成员方法内定义,同时完成实例化的类,若其访问该成员方法中的局部变量,局部变量必须要被final修饰.2. 原因是编译程序实现上的困 ...
- 源码推荐 VVebo剥离的TableView绘制
源码推荐 VVebo剥离的TableView绘制 https://github.com/johnil/VVeboTableViewDemo 此项目由VVebo剥离,希望你能通过这个demo看到我是如何 ...
- 2021羊城杯比赛复现(Crypto)
bigrsa 题目: from Crypto.Util.number import * from flag import * n1 = 10383529640908175186077053551474 ...
- Java静态变量、静态块、构造块、构造函数、main函数、普通代码块的执行顺序
测试代码 public class SingleTest { public static String v = "StaticValue"; static { System.out ...
- Java执行cmd命令、bat脚本、linux命令,shell脚本等
1.Windows下执行cmd命令 如复制 D:\tmp\my.txt 到D:\tmp\my_by_only_cmd.txt 现文件如图示: 执行代码: private static void run ...
- Solution -「CF 1349D」Slime and Biscuits
\(\mathcal{Description}\) Link. 有 \(n\) 堆饼干,一开始第 \(i\) 堆有 \(a_i\) 块.每次操作从所有饼干中随机一块,将其随机丢到另外一堆.求所 ...
- python好用的函数或对象
1.ljust.rjust "hello".ljust(10,"x") #将字符串hello做对齐,并且用字符'x'补到10个字符 #输出为:helloxxxx ...
- MySQL架构原理之存储引擎InnoDB线程模型
如下图示,为InnoDB线程模型示意图: 1.IO Thread 在InnoDB中使用了大量的AIO(Async IO)来做读写处理,这样可以极大提高数据库的性能.其提供了write/read/ins ...