【LeetCode】16. 3Sum Closest 最接近的三数之和
- 作者: 负雪明烛
 - id: fuxuemingzhu
 - 个人博客:http://fuxuemingzhu.cn/
 - 个人公众号:负雪明烛
 - 本文关键词:3sum, three sum, 三数之和,题解,leetcode, 力扣,Python, C++, Java
 
题目地址: https://leetcode.com/problems/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 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).
题目大意
在给定的数组中判断是否存在三个数的和是0,返回所有的组合,但是返回的组合中不能有重复。
解题方法
方法:原数组排序+双指针
这个题和15. 3Sum基本一样,而且这个题更简单一点。
想要得到三个数字的和,要求这个和尽可能的靠近target,那么同样需要先排序,然后使用一个指针遍历,另外两个指针分别指向下一个元素和最后一个元素然后向中间靠拢的方式。在靠拢的过程中如果当前的和与target的差距比要返回的结果与target更小,那么更新要返回的结果。
指针的移动策略是如果和比目标值大,说明我们需要把这个和调小一点;如果和比目标小,那么需要把和调大一点。如果相等那么就返回结果。
时间复杂度是O(N^2),空间复杂度是O(1)。
class Solution(object):
    def threeSumClosest(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        N = len(nums)
        nums.sort()
        res = float('inf') # sum of 3 number
        for t in range(N):
            i, j = t + 1, N - 1
            while i < j:
                _sum = nums[t] + nums[i] + nums[j]
                if abs(_sum - target) < abs(res - target):
                    res = _sum
                if _sum > target:
                    j -= 1
                elif _sum < target:
                    i += 1
                else:
                    return target
        return res
参考资料:
日期
2018 年 10 月 17 日 —— 今又重阳,战地黄花分外香
【LeetCode】16. 3Sum Closest 最接近的三数之和的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
		
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 s ...
 - Leetcode16.3Sum Closest最接近的三数之和
		
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
 - 016 3Sum Closest 最接近的三数之和
		
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
 - LeetCode(16):最接近的三数之和
		
Medium! 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...
 - C#LeetCode刷题之#16-最接近的三数之和(3Sum Closest)
		
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3620 访问. 给定一个包括 n 个整数的 ...
 - LeetCode:最接近的三数之和【16】
		
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
 - Java实现 LeetCode 16 最接近的三数之和
		
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
 - Leetcode题库——16.最接近的三数之和
		
@author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...
 
随机推荐
- linux sort 命令详解(转载)
			
转载:http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html#3374576 sort是在Linux里非常常用的一个命令,管排序的 ...
 - 二叉树——根据遍历结果,画出对应的二叉树 转载至:http://canlynet.blog.163.com/blog/static/255013652009112602449178/
			
这道题目很经典,具体如下: 已知遍历结果如下,试画出对应的二叉树: 前序:A B C E H F I J D G K 中序:A H E C I F J B D K G 解题要点: 1.前序.中序.后序 ...
 - 错误笔记: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable) E: Unable to lock the administration di
			
亲测可用 --jack alexander@alexander-virtual-machine:~$ sudo apt-get install -y httpdE: Could not get loc ...
 - 基于 Golang 构建高可扩展的云原生 PaaS(附 PPT 下载)
			
作者|刘浩杨 来源|尔达 Erda 公众号  本文整理自刘浩杨在 GopherChina 2021 北京站主会场的演讲,微信添加:Erda202106,联系小助手即可获取讲师 PPT. 前言 当今时 ...
 - day12 form组件
			
day12 form组件 今日内容 form组件前戏 form组件基本定义 form组件数据校验功能 form组件渲染标签 form组件提示信息 数据校验进阶 form组件补充 form组件源码探索 ...
 - Type difference of character literals in C and C++
			
Every literal (constant) in C/C++ will have a type information associated with it. In both C and C++ ...
 - Shell脚本实现自动修改IP地址
			
作为一名Linux SA,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者MAC绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名.ip信息.网关等配置.修改成特定的主机名 ...
 - my43_mysql内存相关概念
			
相关参数 read_buffer_size https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_re ...
 - ES6常用的数值转换方法
			
<script type="text/javascript"> // Number常用方法 /* Number.isFinite() 用来检查一个数值是否为有限的(fi ...
 - 莫烦python教程学习笔记——利用交叉验证计算模型得分、选择模型参数
			
# View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...