16. 最接近的三数之和

给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.

与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/3sum-closest

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
public int threeSumClosest(int[] nums, int target) {
// 排序
Arrays.sort(nums);
int closestNum = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length - 2; i++) {
int l = i + 1, r = nums.length - 1;
while (l < r){
int threeSum = nums[l] + nums[r] + nums[i];
if (Math.abs(threeSum - target) < Math.abs(closestNum - target)) {
closestNum = threeSum;
}
if (threeSum > target) {
r--;
} else if (threeSum < target) {
l++;
} else {
// 如果已经等于target的话, 肯定是最接近的
return target;
} } } return closestNum;
}
}

Java实现 LeetCode 16 最接近的三数之和的更多相关文章

  1. [LeetCode] 16. 最接近的三数之和

    题目链接:https://leetcode-cn.com/problems/3sum-closest/ 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 num ...

  2. LeetCode 16. 最接近的三数之和(3Sum Closest)

    题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例 ...

  3. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  4. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

  5. leetcode.数组.16最接近的三数之和-java

    1. 具体题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案 ...

  6. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  7. 【LeetCode】最接近的三数之和【排序,固定k1,二分寻找k2和k3】

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  8. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  9. LeetCode-016-最接近的三数之和

    最接近的三数之和 题目描述:给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...

随机推荐

  1. HDU 2006 (水)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2006 题目大意:给你几个数,求奇数的乘积和 解题思路: 很水,不需要数组的,一个变量 x 就行 代码: ...

  2. git工作中最常用的用法教程,不走命令行

    ·1.1 git的概述 Git(读音为/gɪt/.)是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理.  Git 是 Linus Torvalds 为了帮助管理 Lin ...

  3. linux 修改时间同步到BIOS

    设置时间和日期例如:将系统日期设定成2020年4月14日的命令 命令 : "date -s 04/14/2020" 将系统时间设定成下午5点55分55秒的命令 命令 : " ...

  4. 力扣题解-LCP 06. 拿硬币

    题目描述 桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中.我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数. 示例 1: 输入:[4,2,1] 输出:4 解释: ...

  5. P4015 运输问题 最大/最小费用最大流

    P4015 运输问题 #include <bits/stdc++.h> using namespace std; , inf = 0x3f3f3f3f; struct Edge { int ...

  6. C# 操作Orcle数据库

    1.首先添加NuGet:Oracle.ManagedDataAccess 2.配置连接数据库字符串:Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(H ...

  7. ABAP基础1:概念

    包(Package)也称作开发类,是为了将程序等对象根据功能划分,进行区分和管理而导入的概念 每一个开发对象,如程序.类,都要隶属一个包,包一般隶属于某一个模块 每个包拥有一个CTS号,CTS号用于系 ...

  8. H3C S5500V2交换机误格式化恢复

    一.格式化后,bin文件及视图全部被删除需要联系H3C客服报交换机后面的序列号,然后根据工单中给你的账号密码去H3C官网下载对应的软件包. 二.下载3CDaemon使用TFTP方式将解压出来的.ipe ...

  9. js得到文件后缀

    js得到文件后缀  http://www.cnblogs.com/lan0725/archive/2010/05/25/1873745.html function getFileExt(str) {  ...

  10. IDEA三种注释详解

    三种注释方式 行注释.块注释.方法或类说明注释. 一.快捷键:Ctrl + / 使用Ctrl+ /, 添加行注释,再次使用,去掉行注释 二.演示代码 if (hallSites != null &am ...