题目来源:

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. OSCache报错error while trying to flush writer

    Struts2.3+spring3+hibernate3开发现在想在原有基础上使用 oscache提高性能,使用中发现问题例如:使用struts2标签<cache:cache time=&quo ...

  2. Html 小插件2

    调用google的JS翻译插件实现页面自动翻译功能 网址http://translate.google.com/translate_tools 设置自己需要的配置生成如下代码放到自己站的页面头部 代码 ...

  3. linux shell编程总结

    linux shell编程总结 本周学习了unix/linux shell编程,参考的是<LINUX与UNIX Shell 编程指南>,David Tansley著:徐焱,张春萌等译,由机 ...

  4. UIView 中bounds和frame的差别

    搞iOS开发的童鞋基本都会用过UIView,那他的bounds和frame两个属性也不会陌生,那这两个有什么实质性的区别呢? 先看到下面的代码你肯定就明白了一些: -(CGRect)frame{    ...

  5. 1.2UISwitch 1.3 自定义UIswitch 1.4pickerView

    1.2 UISwitch创建和使用开关 问题你想给你的用户打开一个选项或关闭的能力.解使用UISwitch类. 讨论该UISwitch类提供像在图1-7为自动大写,自动校正,等等所示的开/ ...

  6. SQLHelper简单版(基础版)

    using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...

  7. zoj 1539 Lot

    /*理解题意后,发现最后剩下的都是个数并不是和奇数偶数等有直接的关系,所以我们直接从数量入手 比如11会被分为5,6.5再分2,3.6再分3,3只要剩下三个就算一种,少于三个不用算.大于3个继续分 很 ...

  8. 关于类似于自动填充搜索框的DEMO

    接了个单子,客户要求左边输入时,右边自动到数据库查出对应内容,如果是单个INPUT还好,这个是动态增加INPUT,不过都是一样,关键是思路 这里遇到最郁闷的问题,就是我用的JQ1.9 以前用的JQ1. ...

  9. 关于yield创建协程的理解

    先上利于理解的代码: #coding:utf-8 def consumer(): c_r = '' while 1: m = yield c_r if not m: return print(&quo ...

  10. python的filter()函数

    filter()函数是 Python 内置的另一个有用的高阶函数. filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,fil ...