题目来源:

https://leetcode.com/problems/3sum-closest/


题意分析:

这道题目输入一个数组nums和一个数target,找出数组中三个数,使得他们的和最接近target,返回这三个数的和。


题目思路:

这道题目和上一题3Sum很像,所以也可以用类似的方法去解决这个问题。整个过程分成两步:

①数组排序;这步时间复杂度是(O(nlogn))。

②固定一个数,这步的时间复杂度是(O(n))。

③在剩下的数里面通过“夹逼定理”,找出两个数,使得三个数的和最接近target。这步时间复杂度是(O(n))

总的时间复杂度为(O(nlogn) + O(n)*O(n)) = (O(n^2))。

优化:在第三步的时候通过判断剩下的数中是否最小的两个数相加就大于或者最大两个数就小于target - 第一个数,如果是,则直接判断最小(大)两个数和②中的那个数的和是不是最接近的值。


代码(python):

 class Solution(object):
def threeSumClosest(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
size = len(nums)
if size < 3:
return 0
nums.sort()
i = 0 # fix the first index
ans = nums[0] + nums[1] + nums[size - 1] # ans is used to record the solution
while i < size - 2:
tmp = target - nums[i]
j = i + 1
k = size - 1
while j < k:
if nums[j] + nums[k] == tmp:
return target
if nums[j] + nums[k] > tmp:
if nums[j] + nums[j + 1] >= tmp:
if nums[j] + nums[j + 1] - tmp < abs(ans - target):
ans = nums[i] + nums[j] + nums[j + 1]
break
tmpans = nums[i] + nums[j] + nums[k]
if tmpans - target < abs(ans - target):
ans = tmpans
k -= 1
else:
if nums[k] + nums[k - 1] <= tmp:
if tmp - nums[k] -nums[k - 1] < abs(ans - target):
ans = nums[i] + nums[k - 1] + nums[k]
break
tmpans = nums[i] + nums[j] + nums[k]
if target - tmpans < abs(ans - target):
ans = tmpans
j += 1
i += 1
if ans == target:
return target
return ans

转载请注明出处:http://www.cnblogs.com/chruny/p/4830175.html

[LeetCode]题解(python):016-3Sum Closest的更多相关文章

  1. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  2. No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  3. LeetCode--No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  4. [Leetcode][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

  5. 【LeetCode】016 3Sum Closest

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

  6. 【JAVA、C++】LeetCode 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 ...

  7. [Leetcode]016. 3Sum Closest

    public class Solution { public int threeSumClosest(int[] num, int target) { int result = num[0] + nu ...

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

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

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

随机推荐

  1. Device Mapper Multipath(DM-Multipath)

    Device Mapper Multipath(DM-Multipath)能够将server节点和存储阵列之间的多条I/O链路配置为一个单独的设备.这些I/O链路是由不同的线缆.交换机.控制器组成的S ...

  2. 漫谈程序猿系列:她发现了一个Bug……

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9ydW9r/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  3. SQL Server 2012学习笔记 1 命令行安装

    setup.exe /Q /IACCEPTSQLSERVERLICENSETERMS /ACTION=install /PID=748RB-X4T6B-MRM7V-RTVFF-CHC8H /FEATU ...

  4. objective-C学习笔记(七) 字符串处理

    字符串NSString NSString 是一个Unicode编码,16位字符的字符序列. NSString 是一个类,拷贝时需要注意. 初始化方法:字面量初始化.初始化器.工厂方法. NSStrin ...

  5. mac的svn之cornerstone简易教程

    链接地址:http://jingyan.baidu.com/article/9989c74612a55af648ecfef2.html 背景: 关于cornerstone的介绍很少: 这里介绍mac的 ...

  6. Append和AppendTo

    Append和AppendTo Append向标签中追加内容 AppendTo把所有匹配的元素追加到元素集合中

  7. oracle 11g 物理内存 - 此先决条件将测试系统物理内存总量是否至少为 922MB (944128.0KB)

  8. java 无法割符日期字符串转yyyy-MM-dd hh:mm:ss

    1.需要转换字符串(20150210125942),转化后要达到的目的:2015-02-10 12:59:42 public static void main(String[] args) { Str ...

  9. 制作一个vagrant的win7 box

    准备: 1.win7镜像文件 2.vagrant安装文件 3.virtual box安装文件 步骤: 1.先在本机上安装virtualbox和vagrant,本机为win7,安装虚机也为win7 2. ...

  10. python下读取excel文件

    项目中要用到这个,所以记录一下. python下读取excel文件方法多种,用的是普通的xlrd插件,因为它各种版本的excel文件都可读. 首先在https://pypi.python.org/py ...