LeetCode:16. 3Sum Closest

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 have exactly one solution.
Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

题目的意思就是给出一串整型数字,以及一个目标值,求这串数字中三个数字之和最接近目标值的情况,输出这个和。

思路:从中心向两侧扩展

这一题有点类似于LeetCode:15. 3Sum,只不过第15题是求出和为0的情况。我们还是使用相同的思路,从中心向两侧扩展。如果发现三个数字之和与目标值的差值小于0,则右侧数字向右移动,否则左侧数字向左移动。

Python 代码实现

class Solution:
    def threeSumClosest(self, nums: List[int], target: int) -> int:
        sortNums = sorted(nums)
        print(sortNums)
        l = len(sortNums)
        
        res = 0
    
        minDx = 2147483647 # 最小差距
        # 从中心向两边扩展
        center = 1
        toRightFlag = True # True:right+=1 ; False:left-=1
        for center in range(l-1):
            left = center -1 
            right = center + 1
            
            while left >= 0 and right < l:
                tmpSum = sortNums[left]+sortNums[center]+sortNums[right]
                tmpDx = tmpSum - target
    
                if tmpDx == 0:
                    if abs(tmpDx) < minDx:
                        print("=0",minDx,abs(tmpDx) , tmpSum)
                        minDx = abs(tmpDx)
                        res = tmpSum
                    if toRightFlag:
                        right = right + 1
                    else:
                        left = left -1
  
                if tmpDx < 0:
                    if abs(tmpDx) < minDx:
                        print("<0",minDx,abs(tmpDx) , tmpSum)
                        minDx = abs(tmpDx)
                        res = tmpSum
                    right = right + 1
                    toRightFlag = True
                    
                if tmpDx > 0:
                    if abs(tmpDx) < minDx:
                        print(">0",minDx,abs(tmpDx) , tmpSum)
                        minDx = abs(tmpDx)
                        res = tmpSum
                    left = left -1
                    toRightFlag = False

        return res          

THE END.

深入解析Zynq Ultrascale+ RF Data Converter中的PLL配置与时钟管理 本文深入解析了Zynq Ultrascale+ RF Data Converter中PLL配置与时钟管理的核心原理与实践。文章将RFDC时钟系统比作交响乐团,详细阐述了PLL作为指挥的关键作用,以及系统时钟、Tile时钟、参考时钟与采样时钟的层级关系。重点剖析了PLL配置参数(如VCO频率、分频器设置)的计算方法与稳定性考量,并提供了从IP配置使能顺序到多Tile同步、DRP访问等常见问题的实战避坑指南与调试技巧,帮助工程师构建稳定的射频信号处理基石。 阅读详情

相关推荐

01-SA8155 QNX 虚拟机Hypervisor

Hypervisor运行在Host OS操作系统上,其他操作系统均在Hypervisor上运行。:Hypervisor直接运行在硬件上,操作系统在Hypervisor之上运行。终止(关闭):echo control 0 dev/vmm。路径:qnx_ap/AMSS/platform/vm/resources/关闭/终止 qvm(GVM):slay -s SIGQUIT qvm。", //host PVM --- 主机。不直接操作/dev/vmm 实现关闭,启动vm。

静虚待令,有物混成 1万+

LeetCode 力扣:15.3Sum + 16.3Sum Closest + 18.4Sum 排序 + 双指针遍历 题目+算法分析+Cpp解答

15.3Sum 排序 + 双指针遍历: ​ 首先,将给定的数组进行排序(从小到大的顺序)。 ​ 遍历排序后的数组中的每一个数,如果这个数之前已经讨论过了则可以跳过。 ​ 将取出的数作为“目标”,使用双指针在排序后的数组中找另外的两个数: ​ 一个左指针指向该取出的数的后一个数,即此时的最低位。 ​ 另一个右指针指向数组的最后一个数,即此时的最高位。 ​ 由于数组已经是从小到...

WULH 1177

Android SDK 国内镜像及配置方法(2025最新,包好使!)

android sdk 国内镜像配置方法,纯小白避坑指南,2025最新,亲测包好使!还有包奉上!

土豆是狗的博客 4674

leetcode 16:3Sum Closest with python

import sys class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ nums.sort()...

dxcve的博客 199

第3章 信息技术服务(一)

对 第三章 《信息技术服务》中第一、二、三节 <内涵与外延、原理与组成、服务生命周期>进行总结、归纳

好好学习,天天向上的博客 733

电容式触摸抗干扰设计:工业环境下的实践方案

针对工业环境中电容式touch器件易受干扰的问题,分享实用的硬件布局与软件滤波方案,提升touch响应稳定性和可靠性。

weixin_35755562的博客 843

LeetCode: 16. 3Sum Closest

我的解答: /** * @param {number[]} nums * @param {number} target * @return {number} */ var threeSumClosest = function(nums, target) { var min = Math.abs(nums[0] + nums[1] + nums[2] - target); v...

aboutmn的博客 274

BIOS/UEFI完全指南:从开机自检到超频魔改的底层硬件控制

计算机启动过程始于固件层,其核心是负责硬件初始化与引导的系统软件。传统BIOS基于16位实模式,寻址能力有限,而现代UEFI采用保护模式,支持图形界面与安全启动,实现了从概念到架构的革新。这一演进带来了显著的技术价值:更快的启动速度、更强的安全性以及对大容量存储设备的原生支持。在工程实践中,无论是解决“虚拟化技术禁用”问题、调整内存XMP配置以提升性能,还是为老主板“魔改”添加NVMe支持,都需要深入理解并操作BIOS/UEFI设置。掌握这些底层交互能力,是进行硬件调试、系统优化与故障排查的关键,也是连接抽

weixin_34236869的博客 457

【LeetCode】解题16:3Sum Closest

LeetCode解题 16:3Sum Closest Problem 16: 3Sum Closest [Medium]解题思路Solution (Java) Problem 16: 3Sum Closest [Medium] Given an array nums of n integers and an integer target, find three integers in nums s...

Fayedily的博客 197

5、快速入门

点击启动实例按钮,并设计一个business key,每个实例会有一个business key, 业务主键,也相当于一个流程实例唯一key,一般会与一次业务强相关,比如一个订单号 ,此处取为shopping_1;付款程序完成后会流转到物流发货任务,此时java工程的相应订阅会收到任务,并进入断点,且读到到toWhere变量。单击shopping可以进入实时流程图的执行图,此时流程流转到第一个任务,需要业务系统完成此任务,并通知引擎。添加类,分别订阅流程中的加入购物车任务和物流发货任务。

leyoliu的博客 3501

Synopsys逻辑工艺库(DC综合)

逻辑库包括仅与综合过程有关的信息且通过DC用于设计的综合和优化。这一信息包括引脚到引脚的时序、面积、引脚类型和功耗以及其他DC需要的必须数据。逻辑库是一个文本(通常扩展名".lib"),通过使用Library Compiler(LC)编译生成带有扩展名名".db"的二进制文件。以SMIC的逻辑综合库为例:库文件下载地址。

baidu_34971492的博客 2424

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.

mune_Fly 267

Swin Transformer 类可视化(grad CAM)

完整代码 from pytorch_grad_cam import GradCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM from pytorch_grad_cam.utils.image import show_cam_on_image from torchvision.models import resnet50 import glob from demo import parse_option from models i

G果的博客 6639

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 ...

gt362ll的博客 227

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LeetCode16: 3Sum Closest

和3sum类似,使用2 pointer法 题目链接: 3Sum Closest 1. 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...

暮日落流年的博客 300

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode:16. 3Sum Closest(Medium)

1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数a,b,c,使得a+b+c=sum,sum最接近给定的目标整数target,返回sum。 3. 解题思路 采用与第15题相同的思路(第15题链接),不过要引入两个整型变量min和r...

aysk1112的博客 228

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

[LeetCode][Python]16: 3Sum Closest

# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'16: 3Sum Closesthttps://oj.leetcode.com/problems/3sum-closest/Given an array S of n integers, find three integers in S such that the su...

aaron7909的博客 123

【LeetCode】LeetCode——第16题:3Sum Closest

【LeetCode】LeetCode——第16题:3Sum Closest

hujingshuang 5509

LeetCode16: 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 w...

zero 171

Leetcode 16: 3Sum Closest

class Solution: def threeSumClosest(self, nums: List[int], target: int) -> int: res=0 temp=float('inf') nums.sort() n=len(nums) #[-3, 0, 1, 2] f...

qq_39012149的博客 250

leetcode16:3Sum Closest

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 wo

GODBAR的博客 501

Leetcode: 16. 3Sum Closest

16.3Sum Closest Medium 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 have exactly one solution. Ex...

叶现一的专栏 160

Leetcode1-100: 16. 3Sum Closest

Leetcode1-100: 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

Y123iwantit的博客 295

leetcode:3Sum Closest [16]

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 exact

愤怒的菜鸟专栏 228

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 e...

呦 ^v^-, 写Bug呢? 225

leetcode: 16. 3Sum Closest

Difficulty Medium 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...

lahhass的博客 234
上一篇: LeetCode:15. 3Sum
下一篇: LeetCode:24. Swap Nodes in Pairs
XP-Code
博客等级 码龄12年 113粉丝 194原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值