【LeetCode每天一题】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).
思路
这道题的思路和之前拿到三数求和的思路差不多,都是一个从头开始遍历,另外那个两个指针指向头尾。不同的一点是这里求的是三数之和最接近的选项。所以在逻辑判断处理上稍微有一些不一样。
时间复杂度为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(最接近的三数和)的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- Leetcode16.3Sum Closest最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- [leetcode]16. 3Sum Closest最接近的三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- 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.最接近的三数之和
@author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...
- #leetcode刷题之路16-最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- 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 ...
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
随机推荐
- rx.js 的冷和热观察
http://cn.rx.js.org/manual/overview.html#h213 https://rxjs-cn.github.io/rxjs5-ultimate-cn/content/ho ...
- 智能文件选择列表—— bat 批处理
智能文件选择列表 *.wim @echo off setlocal enabledelayedexpansion title 智能文件选择列表 pushd %~dp0 & cd /d %~dp ...
- linux 下修改键盘映射
参考文档 原因: 输入 键盘原本的 “\ |”,结果映射到 "< >",而 < 与 > 对应的键名分别是 less 与 greater 查看键名: xev ...
- Sqlite数据库管理(SQLite Developer) v4.0.0.528 中文版+破解方法(申明:来源于网络)
Sqlite数据库管理(SQLite Developer) v4.0.0.528 中文破解版 SharpPlus Sqlite Developer 特性: -强大的SQL编辑器 *Sqlite Sql ...
- myEclipse中项目无法部署到tomcat
问题现象: 从svn上新下载了项目到win环境上. 部署项目的时候,在servers视图里,Add Deployment,如下图: 发现只有一个项目可以加载,另外的项目看不到:可是明明我并没有部署过啊 ...
- c语言之要点-泛篇
1.goto goto由goto和标签名组成, 1 1 if(....) 2 2 { 3 3 ..... 4 4 goto part2; 5 5 } 6 6 part2: printf(". ...
- sess.run(tf.global_variables_initializer()) 做了什么?
当我们训练自己的神经网络的时候,无一例外的就是都会加上一句 sess.run(tf.global_variables_initializer()) ,这行代码的官方解释是 初始化模型的参数.那么,它到 ...
- 结构体指针 Pointers to Structures struct Books Book1; struct Books *struct_pointer;
小结: 1.To access the members of a structure using a pointer to that structure, you must use the → ope ...
- User-Defined Variables
mysql> SET @w := SELECT COUNT(*) FROM course WHERE cteacher='程军'; ERROR (): You have an error in ...
- sparking water
1 2 It provides a way to initialize H2O services on each node in the Spark cluster and to access dat ...