[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语言数据结构----栈与递归
本节主要说程序中的栈函数栈的关系以及栈和递归算法的关系. 一.函数调用时的栈 1.程序调用时的栈是也就是平时所说的函数栈是数据结构的一种应用,函数调用栈一般是从搞地质向低地址增长的,栈顶为内存的低地址 ...
- 美国vps哪个比较好,vps国内访问速度最快!
沃网中国是一家成立于2008年的国内idc商,提供基于hyper-v架构的VPS产品,数据中心包括国内电信.美国洛杉矶等,他们采用的是国内访问最快的加州机房ping值,160-180左右相当给力的速度 ...
- C# ADO基础(使用using操作数据库)
1.使用using 来对数据库进行操作,using是资源释放的一种缩写,用于实现了实现了IDisposable接口(释放对象资源的接口是IDisposable) private void button ...
- JavaScript中你可能不知道的九件事
今天凑巧去W3School扫了一遍JavaScript教程,发现从中看到了不少自己曾经没有注意过的细节. 我这些细节列在这里.分享给可能相同不知道的朋友: 1.使用 document.write() ...
- SQL Server 链接数据库 error:40
链接到远程服务器的话,经常犯这个错误,所以做个笔记,省的每次去百度. 1.如果使用的是 local 链接,只需要启动服务就可以了(如下图) 2.如果是远程链接的话,那么是需要启动TCP协议的,步骤如下
- yii教程
http://www.yiichina.com/doc 官网是很好的参考文档
- libcprops
Install Howto Download the latest epel-release rpm from http://dl.fedoraproject.org/pub/epel/6/x86_6 ...
- js 中的bind函数
bind是Function.prototype中内置函数 作用是指定函数作用域 代码参考 http://blog.csdn.net/load_life/article/details/7200381 ...
- Protel 99SE PCB 打印技巧
1. 打开 Protel99SE PCB 设计文档.从菜单File 下单击Print/Preview 打印预览菜单.出现PCB 打印预览介面. 2.从File 下单击 Setup Printer 设置 ...
- nodejs--express开发个人博客(2)
上一部分已经实现了视图的雏形,现在加上逻辑操作. 登陆.注册.文章发表都需要用到数据库的数据存取,用的比较多的就是mongodb了. MongoDB 是一个对象数据库,它没有表.行等概念,也没有固定的 ...