leetcode 16. 3Sum Closest(最近的三个数之和)

MATLAB新手必看:Stewart平台运动学逆解从入门到实战(附完整代码) 本文详细介绍了如何使用MATLAB实现Stewart平台的运动学逆解,从基础原理到完整代码实现,涵盖并联机器人的数学建模、MATLAB编程和Simulink集成。通过实战案例和可视化分析,帮助新手快速掌握Stewart平台的运动学逆解技术,适用于机器人学和控制理论领域的研究与应用。 阅读详情

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example 1:

Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

Constraints:

3 <= nums.length <= 10^3
-10^3 <= nums[i] <= 10^3
-10^4 <= target <= 10^4

给出一个数组,一个target数字,让找出3个数字的和,使得这个和与target最接近
每个数组都有一个解

思路:
涉及到3 Sum的问题,还是用3 Sum的思路,选一个数字,剩下的用2 Sum,以解决遍历所有和的问题
与target最接近表示|sum - target| 越小,注意这个是绝对值的差,所以要用一个变量保存最小的绝对值差,当一个更小的绝对值差出现时,就更新最小绝对值的差,并保存当前3个数字的和
先把数组排序,2 Sum就可以用双指针解决

2 Sum部分用双指针的话,只凭绝对值的差不能判断移动左指针还是右指针,所以移动指针的时候要用3个数字的和与target比较

注意遍历第一个数字的时候只需遍历到数组长度-2,因为保证后面还有至少两个数字

    public int threeSumClosest(int[] nums, int target) {
        if(nums == null || nums.length < 3) {
            return -1;
        }
        
        int result = nums[0] + nums[1] + nums[2];
        int diff = Math.abs(target - result);
        int n = nums.length;
        Arrays.sort(nums);
        
        for(int i = 0; i < n-2; i ++) {
            int left = i + 1;
            int right = n - 1;
            while(left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                int newDiff = Math.abs(sum - target);
                if(newDiff == 0) {
                  return target;
                } else if(newDiff < diff) {
                  diff = newDiff;
                  result = sum;
                }
            
                if(sum < target) {
                    left ++;
                } else {
                    right --;
                }
            }
            
        }
        
        return result;
    }
基于Simulink的自适应反步法(Adaptive Backstepping)控制​ 本文针对非线性系统控制中未知参数和强非线性耦合的挑战,提出基于Simulink的自适应反步法解决方案。通过融合参数在线估计与反步递归设计,该方法显著提升了永磁同步电机(PMSM)速度控制的鲁棒性,使跟踪误差从20%降至3%,参数估计收敛时间小于0.5秒。文章详细阐述了从算法推导到Simulink实现的完整流程,包括系统架构设计、核心算法推导(以PMSM为例)、模块化建模方法、参数优化策略以及工程部署建议。提供的自适应控制器MATLAB代码和关键参数表可直接应用于实际工程,为电机驱动、机械臂等非线性系统控制提 阅读详情

相关推荐

STM32F411CEU6实战:用W25Q64存储多张图片,在240x240 LCD上轮播显示(附源码)

本文详细介绍了如何使用STM32F411CEU6通过SPI接口管理W25Q64 Flash中的多张图片,并在240x240 LCD上实现平滑轮播显示。内容涵盖硬件配置、图片存储方案设计、轮播系统实现及性能优化技巧,特别针对SPI传输优化和多图存储管理提供了实用解决方案。附源码,适合嵌入式开发者参考。

weixin_32025789的博客 312

Leetcode刷题-最接近的三数之和(Java)

如果a+b+c≥target,并且我们知道pb到pc这个范围内的所有数是按照升序排序的,那么如果pc不变而pb向右移动,那么a+b+c 的值就会不断地增加,显然就不会成为最接近target的值了。如果a+b+c<target,并且我们知道pb到pc这个范围内的所有数是按照升序排序的,那么如果pb不变而pc向左移动,那么a+b+c的值就会不断地减小,显然就不会成为最接近target的值了。那么如何进行优化呢?,根据它们与target的值的关系,选择「抛弃」左边界的元素还是右边界的元素,从而减少了枚举的范围。

bxy0806的博客 440

串并装换器的设计及仿真结果

串/并转换是高速数据流处理的重要技巧之一。串/并转换的实现方法多种多样,根据数据的的顺序和数量要求,可以选用寄存器,双扣RAM,SRAM,FIFO,等实现。对于 数量比较小的设计可以采用移位寄存器完成串/并转换。       在工程中,如何体现串/并转换的设计思想呢? 怎样才能提高系统的处理速度呢 ?       一个简单的串/并转换框架如下图所示,串行输入的数据通过FPGA内部的 n 个移

stubben_bear的专栏 2274

10.24-10.28 leetcode基于python刷题

10.24-10.28 leetcode基于python刷题 20. Valid Parentheses(Easy) 3. Longest Substring Without Repeating Characters(Medium) 27. Remove Element(Easy) 16. 3Sum Closest(Medium) 58. Length of Last Word(Easy)5. Longest Palindromic Substring(Medium) 6. Zigzag Conversi

CR1820的博客 364

热视觉2:使用Python和OpenCV从图像中测量温度

这篇博客将介绍学习热/中远红外视觉的基础知识,gray8 和 gray16 图像(即最常见的热成像格式)之间的差异,学习如何使用Python和OpenCV从热图像中的每个像素点测量温度,并以不同的调色板显示结果。以及如何实时计算视频流和UVC热像仪的每个像素温度。

qq_40985985的博客 6712

6.8 点云相关软件——CoProcess

摘要 CoProcess是华测导航研发的三维点云全流程处理平台,支持海量数据浏览、编辑及多类型成果输出,适配地形测绘、道路建设等行业需求。软件采用模块化设计,包含基础功能(数据导入、分类、建模等)和行业定制模块(地形、道路、矿山等),支持多格式数据交互与自动化处理,显著提升作业效率。相比Lidar360,CoProcess操作更简便、性价比更高;相比开源工具CloudCompare,其行业适配性与专业功能更优。2025版本优化了数据处理性能与智能算法,需授权激活高级功能,适合商业项目应用。

qq625924821的博客 249

算法:最接近的三数之和(3sum-closest)。

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

孤芳不自赏 1266

HighTec安装使用

HighTec安装使用

qq_43552564的博客 3836

最近的三数之和

Leecode刷题 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。 示例 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2)...

我本傲骄的博客 411

PyTorch深度学习实战(2)——PyTorch基础

PyTorch 是广泛应用于机器学习领域中的强大开源框架,因其易用性和高效性备受青睐。在本节中,将介绍使用 PyTorch 构建神经网络的基础知识。首先了解 PyTorch 的核心数据类型——张量对象。然后,我们将深入研究用于张量对象的各种操作。PyTorch 提供了许多帮助构建神经网络的高级方法及组件,并提供了利用 GPU 更快地训练神经网络的张量对象。

盼小辉丶的博客 5万+

TOGAF架构内容—TOGAF 内容框架和企业元模型

TOGAF架构内容—TOGAF 内容框架和企业元模型

深圳市多克创新科技有限公司 1664

【LeetCode】最接近的三数之和(三数之和)

题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。 暴力 int threeSumClosest(vector<int>& nums, int target) { vector<int> sums; for (int i = 0; i < nums.size()-2; i++) { for (int j = i + 1;

weixin_44302602的博客 342

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LeetCode 16. 3Sum Closest (Java版; Medium)

welcome to my blog LeetCode Top Interview Questions 16. 3Sum Closest (Java版; Medium) 题目描述 Given an array nums of n integers and an integer target, find three integers in nums such that the sum i...

littlehaes的博客 363

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LeetCode 16 3Sum Closest 找出最接近指定target的三个数的和

题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would hav

大三狗现在才努力还不算晚吧?! 2391

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

[leetcode]16. 3Sum Closest

Description Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each ... 最接近的三数之和的C++和Python实现。

Learning from the mistakes 345

[LeetCode 16] 3Sum Closet

Given an arraynumsofnintegers and an integertarget, find three integers innumssuch that the sum is closest totarget. Return the sum of the three integers. You may assume that each input would...

ExcitedZhang的博客 204

LeetCode16. 3Sum Closest 三数之和改

16.3Sum Closest最接近目标值的三数之和 3Sum Closest Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integer...

goasleep的博客 173

LeetCode总结——从2Sum、3Sum、3Sum Closest、4Sum到kSum

leetcode求和问题描述(K sum problem): 给你一组N个数字(nums), 然后给你一个常数(target) ,我们的目标是在这一堆数里面找到K个数字,使得这K个数字的和等于target。 注意事项(constraints): 注意这一组数字可能有重复项:比如 1 1 2 3 , 求3sum, 然后 target = 6, 你搜的时候可能会得到 两组1 2 3, 1 2 3,1...

vivian_ll的博客 382

LeetCode-16. 3Sum Closest

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would ...

qq_32350719的博客 209

leetcode16 最近三数之和

题目链接:https://leetcode-cn.com/problems/3sum-closest/ 总结: 题目类似15,但是需要求最近,同样变换等式,然后判断三个数字的和与target之间的差,如果大于target那么说明正数比较大,那么r–,反之l++,每次循环都执行这个操作 加油 // // Created by zhangfeng on 2019-07-02. // // a +...

Zephyr的博客 208

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

题干: 效果: 思路: 这道题跟No.15很像,就是要找三数之和,但15是找三数之和为0,这题是找离target最近的和。 首先我们得排序,因为调用默认排序的时间复杂度是O(nlogn),而这道题暴力遍历是O(n^3),想处理三个数,至少也得是O(n^2)的复杂度,所以先给他排个序对时间复杂度不会有大的影响。 从小到大排完序后,前两个从前往后取,第三个从最后位置往前取,这样就有一个好处,我们把...

一条菜狗的博客 1019

LeetCode 16题. 3Sum Closest

Given an arraynumsofnintegers and an integertarget, find three integers innumssuch that the sum is closest totarget. Return the sum of the three integers. You may assume that each input would ...

Autumncow的博客 331

LeetCodet题解--16. 3Sum Closest(离给定值最近的三数之和)

链接 LeetCode题目:https://leetcode.com/problems/3sum-closest/ GitHub代码:https://github.com/gatieme/LeetCode/tree/master/016-3SumClosest 优快云题解:http://blog.csdn.net/gatieme/article/details/510566

OSKernelLAB(gatieme) 2026

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 number, target. Return the sum of the three integers. You may assume that each input would have exactl

指尖飞舞 1105

XS5018A同轴高清ISP芯片:模拟监控升级与调试实战指南

在安防监控与车载影像领域,ISP图像处理技术是决定画质的核心环节。传统模拟系统受限于CVBS带宽,画质难以提升,而同轴高清技术通过AHD/TVI/CVI协议在现有同轴电缆上传输1080P甚至更高分辨率的视频信号,为老旧模拟系统提供了低成本升级路径。XS5018A作为一款集成ISP与同轴视频发射功能的芯片,负责从CMOS Sensor RAW数据采集到模拟高清信号输出的完整图像处理流程,涵盖坏点矫正、去马赛克、降噪、宽动态等关键模块。其技术价值在于无需网络布线即可实现高清化改造,具备低延迟、抗干扰等优势,适用

weixin_34408717的博客 525

【每日一题】LeetCode. 16. 最接近的三数之和

每日一题,防止痴呆 = = 一、题目大意 给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/3sum-closest 二、题目思路以及AC代码 思路 考虑这题的话,首先想到三数之和,其实没有什么特殊的方法,也就只能枚举了,所以我们就要考虑如何才能对其进行有效的枚举.

Johnson的博客 271
上一篇: leetcode 15. 3Sum(三个数之和)
下一篇: leetcode 18. 4Sum(四个数之和)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值