# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closest
https://oj.leetcode.com/problems/3sum-closest/ Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). ===Comments by Dabay===
先排序,然后从左到右固定一个数,在后边的数列中使用左右指针往中间靠拢的方法查找。
当比之前更接近target的时候,更新找个这个值。 从左往右固定一个数,左右两个指针往中间靠拢。
'''
class Solution:
# @return an integer
def threeSumClosest(self, num, target):
if len(num) < 3:
return []
num.sort()
closest = num[0] + num[1] + num[2]
difference = abs(target - closest)
for i in xrange(len(num)-2):
j, k = i+1, len(num)-1
while j < k:
sum = num[i] + num[j] + num[k]
if abs(target - sum) < difference:
closest = sum
difference = abs(target - sum)
if sum == target:
return sum
elif sum < target:
j = j + 1
else:
k = k - 1
return closest def main():
sol = Solution()
nums = [-3,0,1,2]
solution = sol.threeSumClosest(nums, 1)
print solution if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[LeetCode][Python]16: 3Sum Closest的更多相关文章

  1. 《LeetBook》leetcode题解(16):3Sum Closest [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  3. 【一天一道LeetCode】#16. 3Sum Closest

    一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...

  4. Leetcode Array 16 3Sum Closest

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

  5. 【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 ...

  6. LeetCode:16. 3Sum Closest(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...

  7. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  8. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  9. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

随机推荐

  1. Three-Color Flag

    问题陈述: 三色旗的问题最早由E.W.Dijkstra所提出,他所使用的用语为Dutch Nation Flag(Dijkstra为荷兰人),而多数的作者则使用Three-Color Flag来称之. ...

  2. JQeury Image LazyLoad

    使用jquery插件实现图片延迟加载技术 http://www.cnblogs.com/szytwo/archive/2012/12/27/2836141.html EasyUI http://www ...

  3. android,在fragment中使用listview,不使用listfragment

    public class LeftFragment extends Fragment{ private ListView listView; @Override public View onCreat ...

  4. Android 微信分享信息

    随着微信越来越火,越来越多的应用要求有分享到微信的功能.虽然有很多平台都帮集成有分享功能,比如友盟.但是个人觉得友盟集成的东西太多了,自己封装得太过分了,很多资源文件也要带进去,所以感觉不是怎么好,所 ...

  5. js获得url内的参数

    /** * js获得url内的参数 * 返回值: string or undefined * 使用方法:$_GET['from'] */ var $_GET = (function() { var s ...

  6. Win7 扩容磁盘分区

    1.计算机->管理->磁盘管理,磁盘颜色代表意义 主分区:深蓝色: 扩展分区 :绿色的框: 逻辑分区:浅蓝色的分区: 可用空间:绿色分区 2.非主分区扩容 非主分区扩容十分简单,可是须要注 ...

  7. android 计时器,倒计时

    自己定义CountDownTimer /** * 计时器 * @author Administrator * */ class TimeCount extends CountDownTimer{ pu ...

  8. 【小白的java成长系列】——java ide 开发工具eclipse的操作

    今天看了一下自己的博客文章,里面的内容还是比較杂的,有好多技术,有好多语言,都没有突出自己的强项,能够说,从博客里面,看不出我究竟是做哪块的..加上今天被授予了博客准专家勋章,自己想了一下,还是得梳理 ...

  9. android实现点击短链接进入应用 并获得整个连接的内容

    例如:我收到一个短信是一个链接:http://www.qq.com/a/b/?, 当我点击以后我要实现的场景是:点击----->有两种情况可供选择:手机中有此应用,跳转到应用:反之,则进入网页. ...

  10. 略谈cpu架构种类

    一直对x86/i386/i686/x86_64这些东西感觉很不清楚,查些资料,解决部分问题,小记一番. Question1:什么是x86? x86或80x86是英特尔Intel首先开发制造的一种微处理 ...