[LeetCode][Python]16: 3Sum Closest
# -*- 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的更多相关文章
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【一天一道LeetCode】#16. 3Sum Closest
一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...
- 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 ...
- 【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(Medium)
1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 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 ...
随机推荐
- EditText 空指针问题
今天在Android中碰到了这样一个问题,其实应该很少人会碰到,因为只有像我这种奇葩才会犯这种错误. 但既然解决了,我就想在这里跟大家分享一下,毕竟它困扰了我一个白天啊...不多说了,看下面... 其 ...
- Android-Ant自动编译打包android项目 -- 2 ----签名与渠道包
上篇介绍了怎么使用ant自动编译打包现有的android项目,这篇将继续介绍如果如何在ant打包应用的时候加入签名信息以及自动打包渠道包. 1. 加入签名信息: 在项目的根目录下建一个ant.prop ...
- OpenLayers访问WTMS服务及添加Googlemap
1.访问WMS服务 首先需要发布WMS服务,才能进行地图WMS服务访问.这里不说怎么发布WMS服务,直接看怎么调用,代码如下: 代码 Code highlighting produced by Act ...
- jquery 中 fn.apply(this, arguments)是什么函数?有什么作用?能举个例子吗
function Person(name){ this.name=name; this.sayname=function (){ alert(this.name); } } function Stud ...
- Jquery操作-(多种实例)--未完
一.Jquery简介 定义 jQuery创始人是美国John Resig,是优秀的Javascript框架: jQuery是一个轻量级.快速简洁的javaScript库. jQuery对象 jQuer ...
- swift 用协议实现代理传值功能
1.功能简介 RootViewController中用个lable和一个按钮,点击按钮跳转到模态窗口.在模态窗口中有个TextField和一个按钮,输入文字点击关闭模态按钮后跳转到RootViewCo ...
- iOS获取本地ip(基本通用)
今天有个朋友问我怎样訪问手机ip,上网找了几个,用了近200多行代码,最后发现头文件用的居然还是Linux中的,OC没有这个头文件.感觉socket本身应该能够后去自己的ip就试了一下,果然7.8行代 ...
- 无法加载协定为“XXXWebServiceSoap”的终结点配置部分,因为找到了该协定的多个终结点配置
错误描述:无法加载协定为“XXXWebServiceSoap”的终结点配置部分,因为找到了该协定的多个终结点配置.请按名称指示首选的终结点配置部分. 错误原因:该webservce在web.confi ...
- CentOS6.4下搭建hadoop2.2(64bit)注意事项
注:本文针对64位机器,32bit课直接tar -zxvf hadoop-2.2.0.tar.gz 解压配置即可. Step1:安装jdk(6以上版本) Step2:下载hadoop--->ht ...
- JS 精粹(一)
先说说JS是容易被误解的语言的观点.从名字上来看,"Java"这似乎暗示着这门语言与Java的关系:好像这门语言是Java的子集,或比Java弱.但实际与Java并无关系,如果说非 ...