最接近的三数之和

你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数,使它们的和与 target 最接近。

返回这三个数的和。

假定每组输入只存在恰好一个解。

示例 1:

输入:nums = [-1,2,1,-4], target = 1

输出:2

解释:与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。

示例 2:

输入:nums = [0,0,0], target = 1

输出:0

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int[] nums = {-1, 2, 1, -4};
if (nums.length < 3) {
System.out.println("不符合题意的数组大小");
return;
}
int threeSumClosest = threeSumClosest(nums, 1);
System.out.println(threeSumClosest);
} /**
* 求出最接近目标值target的三数之和
*
* @param nums 要传入的数组
* @param target 目标值
* @return 最接近目标值target的三数之和
*/
public static int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int ans = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length; i++) {
// 前置位置startIndex,后置位置endIndex
int startIndex = i + 1, endIndex = nums.length - 1;
while (startIndex < endIndex) {
int sum = nums[startIndex] + nums[endIndex] + nums[i];
// 更新最接近的值,通过比较,获取到最接近target的值
if (Math.abs(target - sum) < Math.abs(target - ans)) {
ans = sum;
}
// 如果求和值大于目标值,后置位置往前挪
if (sum > target) {
endIndex--;
} else if (sum < target) {
// 如果求和值小于目标值,后置位置往前挪
startIndex++;
} else {
// 如果等于目标值,直接返回
return ans;
}
}
}
return ans;
}
}

LeetCode - 最接近的三数之和的更多相关文章

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

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

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

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

  3. Java实现 LeetCode 16 最接近的三数之和

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

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

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

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

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

  6. lintcode-59-最接近的三数之和

    59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...

  7. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

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

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  9. LeetCode(16):最接近的三数之和

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

  10. #leetcode刷题之路16-最接近的三数之和

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

随机推荐

  1. 在Visual Studio 中使用git系列文章目录

    在Visual Studio 中使用git--什么是Git(一) 在Visual Studio 中使用git--给Visual Studio安装 git插件(二) 在Visual Studio 中使用 ...

  2. Zabbix6.0使用教程 (三)—zabbix6.0的安装要求

    接上篇,我们继续为大家详细介绍zabbix6.0的使用教程之zabbix6.0的安装部署.接下来我们将从zabbix部署要求到四种不同的安装方式逐一详细的为大家介绍.本篇讲的是部署zabbix6.0的 ...

  3. 异步小工具 asyncTool

    class asyncTool { constructor () { this.arr = [] this.ctx = {} } use (func) { const into = { func, n ...

  4. Shell脚本自动下载FTP文件上传到S3

    1. shell脚本下载 #!/bin/bash #用户名 USER=xxx #密码 PASSWORD=xxx #下载文件临时目录 SRCTDIR=/approveform/uat/tempin #S ...

  5. 一个简单的RTMP服务器实现 --- RTMP实现要点

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...

  6. 03.Android崩溃Crash库之ExceptionHandler分析

    目录总结 00.异常处理几个常用api 01.UncaughtExceptionHandler 02.Java线程处理异常分析 03.Android中线程处理异常分析 04.为何使用setDefaul ...

  7. uni组件传值注意

    目录介绍 01.组件传值遇到坑 02.父组件传值给子组件 03.子组件传值给父组件 01.组件传值遇到坑 子组件给父组件传值注意点 注意子组件触发事件定义的方法,首先在父组件中需要绑定子组件内部对应事 ...

  8. 记录--CSS 滚动驱动动画 scroll()

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 CSS 滚动驱动动画 scroll() animation-timeline 通过 scroll() 指定可滚动元素与滚动轴来为容器动画提 ...

  9. read IEEE standard for verilog(3)

    read IEEE std for verilog 1.阅读准备 在阅读的第二部分读到了lexical conventions,这次计划读一节.也就是把lexical conventions读完. 2 ...

  10. Python 列表list方法clear( )和直接list [ ]的区别

    x.clear()是将内存地址清空, x=[ ]会新开辟一个内存空间.