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. 第八天py

    我发现这个老师讲的太基础只是我懒得跳过而已想慢慢听 列表公共功能 索引 切片 for 长度 特有功能 翻转 排序 追加 插入 索引位置 删除

  2. Eclipse中代码格式化配置

    一.配置formatter 从Eclipse主菜单选择“窗口→首选项”,进入“代码格式化程序”设置页.如下图所示: 确认选择的是格式化配置是Eclipse [built-in]. 注意:编写好代码后需 ...

  3. F#周报2018年第50期

    新闻 Bolero: 用于WebAssembly的F#工具 Ionide-fsharp安装数量超过10万 WPF的Xaml.Behaviors类库开源 Visual Studio 2019预览版 .N ...

  4. CH 4701 - 天使玩偶 - [CDQ分治]

    题目链接:传送门 关于CDQ分治(参考李煜东<算法竞赛进阶指南>): 对于一系列操作,其中的任何一个询问操作,其结果必然等价于:初始值 + 此前所有的修改操作产生的影响. 假设共有 $m$ ...

  5. 使用Zookeeper命令的简单操作步骤

    (1) 使用ls命令查看当前Zookeeper中所包含的内容:ls / [zk: localhost:2181(CONNECTED) 1] ls / [zookeeper] [zk: localhos ...

  6. deepin中Tomcat添加执行权限

    terwer@terwer-PC:~$ cd /opt/*tomcat*/bin terwer@terwer-PC:/opt/apache-tomcat-9.0.13/bin$ sudo chmod ...

  7. 于dm-0 dm-1

    dm是device mapper的意思,dm-0, dm-1的实体可以通过下面几个命令看出,lvm会把每个lv连接到一个/dev/dm-x的设备档,这个设备档并不是一个真正的磁盘,所以不会有分区表存在 ...

  8. linux 记录所有用户bash操作日志

    记录所有用户登录系统的任何操作日志,以便有据可查. 1.编辑 /etc/profile文件.   1 # vim /etc/profil 2. 在其后添加如下内容   1 2 3 4 5 6 7 8 ...

  9. java发送get,post请求

    方法里面有注释:参照csdn里面的,项目用时自己改 package com.bst.express; import java.io.BufferedReader; import java.io.Dat ...

  10. python摸爬滚打之day05----字典

    1.字典介绍 1.1  结构:  {key1: value1, key2: value2, ....} ,由很多键值对构成. 在字典的key-value(键值对)中, key(键)必须是可哈希(不可变 ...