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

思路


  这道题的思路和之前拿到三数求和的思路差不多,都是一个从头开始遍历,另外那个两个指针指向头尾。不同的一点是这里求的是三数之和最接近的选项。所以在逻辑判断处理上稍微有一些不一样。

  时间复杂度为O(n2),空间复杂度为O(1)。

解决代码


 class Solution(object):
def threeSumClosest(self, nums, target):
nums.sort()
min_size = 99999 # 用来记录最接近和的值的变量
result = 0 # 记录最接近的三个数之和
if len(nums) < :
return
leng = len(nums)
for i in range(leng-): # 第一个开始遍历
if i > and nums[i] == nums[i-]: # 如果相同则直接跳过。
continue
start,end = i+, leng-1 # 设置首位两个指针
while start < end:
tem =nums[i] + nums[start] + nums[end]
if tem - target < : # 如果三数之和减去target 小于0, 然后反过来判断是否小于min_size的值, 满足的话更新result和min_size
if target - tem < min_size:
result = nums[i] + nums[start] + nums[end]
min_size = target - tem
start +=
else: # 如果三数之和减去target大于0, 然后同理进行判断
if tem - target < min_size:
result = nums[i] + nums[start] + nums[end]
min_size = tem - target
end -=
return result

【LeetCode每天一题】3Sum Closest(最接近的三数和)的更多相关文章

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

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

  2. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  3. Leetcode16.3Sum Closest最接近的三数之和

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

  4. [leetcode]16. 3Sum Closest最接近的三数之和

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

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

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

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

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

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

  8. leetcode第16题--3Sum Closest

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

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

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

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

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

随机推荐

  1. (ecj)Eclipse的Java编译器分析之一——ecj介绍

    Java是一个开放的平台,对于除发布编译器/解释器/基础类库之外,该语言的负责机构更多的是制定一系列标准,任何符合标准的厂商产品均可用于市场投放.甚至包括其编译器及解释器. (比如Hibernate提 ...

  2. Flask----目录结构

    以此结构为例,这个小项目是<Flask Web开发:基于python的web应用开发实战>第一部分结束后的代码框架 第一层 有app.tests.migrations三个文件夹和confi ...

  3. [No0000147]深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing)理解堆与栈4/4

    前言   虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC).另外,了解内存管理可以帮助我们理解在每一个程 ...

  4. Chap3:区块链的衍生技术[《区块链中文词典》维京&甲子]

  5. [knowledge][lisp] lisp与AI

    https://blog.youxu.info/2009/08/31/lisp-and-ai-1/ https://blog.youxu.info/2010/02/10/lisp-and-ai-2/

  6. synchronized使用

    在一个方法内部使用如下代码: public void m5() { synchronized (Test1.class) { System.out.println("m5"); t ...

  7. Python pip 如何升级

    场景:部署环境时,在线安装第三方库(pip install flask-bootstrap),提示pip版本过低. 解决方法一:        命令: python -m pip install -- ...

  8. golang 死锁

    golang中for{}会引起程序死锁 如: main(){ go func(){fmt.Println("dfkdsf")} for{ } } 程序运行一会会停止 按照下面的写法 ...

  9. C# 解构

    我们以前用ref或者out在一定程度上可以解决方法只有一个返回值的问题.在C#7.0中新增了一个新元组(ValueTuple),他可以让我们返回多个值.话不多说,先上代码: 我们可以看到可以用隐式推断 ...

  10. BZOJ3613 南园满地堆轻絮 二分/贪心

    正解:贪心 解题报告: 传送门! 这题似乎是可以二分水过的,,,但数据可以加强一下就能简单把二分卡住了,或者修改下空间限制什么的反正就很容易能卡住 所以这里介绍一个优秀的贪心做法,O(n)的时间复杂度 ...