[LeetCode]题解(python):016-3Sum Closest
题目来源:
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的更多相关文章
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- LeetCode--No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- 【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 ...
- 【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 ...
- [Leetcode]016. 3Sum Closest
public class Solution { public int threeSumClosest(int[] num, int target) { int result = num[0] + nu ...
- 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)3Sum Closest
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 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 ...
随机推荐
- 转 C语言面试题大汇总
转 C语言面试题大汇总,个人觉得还是比较全地!!! \主 题: C语言面试题大汇总,个人觉得还是比较全地!!! 作 者: free131 (白日?做梦!) 信 誉 值: 100 ...
- POJ 1700 经典过河问题(贪心)
POJ题目链接:http://poj.org/problem?id=1700 N个人过河,船每次最多只能坐两个人,船载每个人过河的所需时间不同,问最快的过河时间. 思路: 当n=1,2,3时所需要的最 ...
- swith
“开关”(Switch)有时也被划分为一种“选择语句”.根据一个整数表达式的值,switch语句可从一系列代码选出一段执行.它的格式如下: switch(整数选择因子) { case 整数值1 : 语 ...
- XML DOM 节点
来自:w3cschool菜鸟教程 在 DOM 中,XML 文档中的每个成分都是一个节点. DOM 节点 根据 DOM,XML 文档中的每个成分都是一个节点. DOM 是这样规定的: 整个文档是一个文档 ...
- java String.Format详解
JDK1.5中,String类新增了一个很有用的静态方法String.format(): format(Locale l, String format, Object... args) 使用指定的语言 ...
- linux下emacs安装
1.下载地址:http://ftp.gnu.org/pub/gnu/emacs/ 下载文件:emacs-24.2.tar.gz 步骤: 一.安装依赖文件: (先进入root:终端中输入 su -) ...
- video标签 拖动 转自w3school
调整视频大小 播放 暂停 用js实现 详细参见http://www.w3school.com.cn/tiy/t.asp?f=html5_video_dom 图片的拖动详见http://www.w3sc ...
- Spring的事务属性
1.事务Transactional下的属性 @Transactional( readOnly = false, // 读写事务,只读事务 timeout = -1, // 事务的超时时间不限制 //n ...
- virtualbox中新版本Ubuntu安装软件增强包后重启无限登录界面的解决办法
原来我虚拟机版本是4.2.10,装的Ubuntu3.3,因为版本过老使用出现了一些问题,于是换成14.04,安装成功,但是装增强包的时候,装完重启,无限登录界面,密码是对的. 看了网上的很多方法,什么 ...
- java 无法割符日期字符串转yyyy-MM-dd hh:mm:ss
1.需要转换字符串(20150210125942),转化后要达到的目的:2015-02-10 12:59:42 public static void main(String[] args) { Str ...