class Solution(object):
def threeSumClosest(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if len(nums) <= 2:
return False
nums.sort()
res=nums[0]+nums[1]+nums[2]
for i in range(len(nums)-2):
left=i+1
right=len(nums)-1
while left < right:
cur=nums[i]+nums[left]+nums[right] if abs(cur-target) < abs(res-target):
res=cur
if res == target:
return res
elif cur > target:
right-=1
else:
left+=1 return res

leetcode 3Sum Closest python的更多相关文章

  1. [LeetCode]3Sum Closest题解

    3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...

  2. [LeetCode] 3Sum Closest 最近三数之和

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

  3. Leetcode 3Sum Closest

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

  4. LeetCode 3Sum Closest (Two pointers)

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

  5. LeetCode——3Sum Closest

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

  6. LeetCode 3Sum Closest 最近似的3sum(2sum方法)

    题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...

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

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

  8. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  9. 3Sum Closest - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...

随机推荐

  1. JavaScript判断图片是否加载完成

    一.load事件 <!DOCTYPE HTML><html> <head>     <meta charset="utf-8">   ...

  2. Hadoop书籍下载链接

    Hadoop书籍推荐1:Hadoop实战(结合经典案例全面讲解hadoop整个技术体系)http://www.db2china.net/club/thread-25148-1-1.html2:Hado ...

  3. NLS_COMP和NLS_SORT参数

    Oracle默认是采用binary进行排序,这对于例如中文的排序来说,是不恰当的.使用这两个参数可以指定排序的方法,比如拼音或是,要注意可能会引起性能问题.解决方法是使用NLSSORT函数来建立一个函 ...

  4. java中的302和sendRedirect的区别

    java中有一个sendRedirect函数这个用于跳转到另外一个页面,这个实际上是一个302跳转,但是不完全等同于302跳转 点击(此处)折叠或打开 response.sendRedirect(&q ...

  5. UVA 1001 Say Cheese

    题意: 一只母老鼠想要找到她的玩具,而玩具就丢在一个广阔的3维空间上某个点,而母老鼠在另一个点,她可以直接走到达玩具的位置,但是耗时是所走过的欧几里得距离*10s.还有一种方法,就是靠钻洞,洞是球形的 ...

  6. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  7. Javascript 拖拽雏形中的一些问题——逐行分析代码,让你轻松了解拖拽的原理

    今天我们就来解决上一次拖拽雏形中的一些问题.下面看看有哪些问题? 附上上期的Javascript代码,方便大家查看问题. <script type="text/javascript&q ...

  8. SQL Server MYSQL 对外键建立索引的必要性

    背景: 大家知道在定义外键时,都会给出on delete .....   on update .....: 这里指定的就是当主表的列发生变化时,从表的列要用怎么样的变化去迎合.对从表中的外键,建立索引 ...

  9. VS2008下WinRar源码生成dll和 lib总结

    WinRar官方提供了源码(http://www.rarlab.com/rar_add.htm):如果自己想要修改里面的内容就要重新生成DLL和LIB,我在网上找了很多资料都没有说得很清楚.花一两天的 ...

  10. zookeeper 手动T掉已挂节点

    zjtest7-redis:/root/zk# cat test_zk.pl use ZooKeeper; use AnyEvent; use AE; use Data::Dumper; use IO ...