[算法题] 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).
题目思路
题目难度:medium
本题在3sum的基础上稍稍变了一下。在3sum中,返回的条件是target等于三个数的和,而在这里返回的条件是三个数的和到target的距离最近。因此本题和3sum的区别在于:需要调整一下返回值的取法。而通过双指针法的思想不变。
Python代码
class Solution(object):
def threeSumClosest(self, nums,target):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums=sorted(nums)
_len=len(nums)
the_min=999999
res=0
for i in xrange(_len-2):
tar=nums[i]
left=i+1
right=_len-1
while left<right:
_sum=nums[left]+nums[right]
if abs(_sum+tar-target)<the_min:
res=nums[i]+nums[left]+nums[right]
the_min=abs(_sum+tar-target)
if _sum+tar-target<0:
left+=1
else:
right-=1
return res
[算法题] 3Sum Closest的更多相关文章
- 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 ...
- [算法题] 3Sum
题目内容 题目来源:LeetCode Given an array S of n integers, are there elements a, b, c in S such that a + b + ...
- 算法(6)3Sum Closest
kSum问题是一类问题,基本的方法是两个循环,其他一个查找,但是今天碰到了一个超级棘手的问题,是3Sum问题的一个变型 问题:给定一个数组,给定一个整数k,要求找出在数组中找到3个整数,使得这三个整数 ...
- 算法题丨3Sum
描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- LeetCode算法题-Maximize Distance to Closest Person(Java实现)
这是悦乐书的第328次更新,第351篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第198题(顺位题号是849).在一排座位中,1表示一个人坐在该座位上,0表示座位是空的 ...
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- 2Sum,3Sum,4Sum,kSum,3Sum Closest系列
1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...
- 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 ...
随机推荐
- Ext viewport的渲染
Ext viewport的渲染 1.在app.js里创建 Ext.application({ name: 'MySecurity', extend: 'MySecurity.Application', ...
- C#调用TSC条码打印机打印二维码
#region 调用TSC打印机打印 /// <summary> /// 调用TSC打印机打印 /// </summary> /// <param name=" ...
- springmvc4.0配置ajax请求json格式数据
1.导入相关jar包:jackson-annotation-2.5.4.jar,jackson-core-2.5.4.jar,jackson-databind-2.5.4.jar. 2.spring- ...
- (转载)jConsole,jvisualvm和jmap使用
原文链接:http://my.oschina.net/freegarden/blog/286372 摘要 Oracle JVM自带了一些工具,观察java程序的运行,用于排错调优.正文将会对 jCon ...
- webpack1 新手入门教程
本文github仓库地址: https://github.com/Rynxiao/webpack-tutorial ,里面包括了本教程的所有代码. [如果你觉得这篇文章写得不错,麻烦给本仓库一颗星:- ...
- 孤儿文档是怎样产生的(MongoDB orphaned document)
使用MongoDB的开发人员应该都听说过孤儿文档(orphaned document)这回事儿,可谓闻着沉默,遇者流泪.本文基于MongoDB3.0来看看怎么产生一个orphaned document ...
- gulp-rev-append静态资源添加版本号后缀,清理缓存
大多用的是gulp-rev.gulp-rev-collerctor两个插件,但过程有点麻烦,使用gulp-rev-append插件轻松搞定 github: https://github.com/b ...
- 业余草教你解读Spark源码阅读之HistoryServer
HistoryServer服务可以让用户通过Spark UI界面,查看历史应用(已经执行完的应用)的执行细节,比如job信息.stage信息.task信息等,该功能是基于spark eventlogs ...
- ES6 新增命令
let 用来声明变量.它的用法类似于var,但是所声明的变量, 只在let命令所在的代码块内有效. 例: {var a=10; let b=20;}; console.lo ...
- 关于delete使用limit的一些注意事项
在使用delete删除记录时,如果表里面存在多条相同的记录,但是此刻你只想删除一条记录,那么limit就派上了用场.但是使用limit的时候得注意: 如图,您如果想着删除第一个名字叫做张三的,如果你这 ...