# -*- 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 ...
随机推荐
-
Applicationpoolidentity 好有趣哦
前言: 在服务器上搭建一个网站 Windows Server2008 R2 +IIS7. 应用程序池默认选择ApplicationPoolIdentity.为了适应分布式需求,所以将SqlServer ...
-
ANT 发布项目中 build.xml 文件的详细配置
xml 代码 <?xml version="1.0" encoding="UTF-8"?> <!-- name:对应工程名字 default: ...
-
C#和.net之间的关系
What is the difference between C# and .NET? In addition to what Andrew said, it is worth noting that ...
-
BEANUTIL 对象转JSON
package cn.com.softmap.cache.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutp ...
-
TestNG中的DataProvider返回Iterator<;Object[]>;的妙用
TestNG中使用DataProvider有个好处,就是: 1. 在执行用例的时候dataProvider迭代中的每组数据都是作为一个用例执行 2. 在测试数据有规律的情况下,免去了添加testXML ...
-
修改UITextField的占位文字颜色的三种层次
层次一:利用富文本 // 描述占位文字属性 NSMutableDictionary *dict = [NSMutableDictionary dictionary] ; dict[NSForegrou ...
-
Vue-表单输入绑定
>>>>>>> html <div id="app" > <!-- 输入框绑定 --> <input v-m ...
-
静态IP设置
先查看自动网络的ip地址,然后设置 cmd进入DOS输入命令:ipconfig /all 设置固定IP
-
Application对象及常用方法
Application对象: 服务器启动后,就产生了这个application对象.当一个客户访问服务器上的一个JSP页面时,JSP引擎为该客户分配这个 application对象,当客户在所访问的网 ...
-
OsWatcher 使用详解
软件下载地址: https://support.oracle.com/epmos/faces/DocumentDisplay?_afrLoop=520996062954556&id=30113 ...