[算法题] 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 ...
随机推荐
- php下redis的安装教程
1.得有php的环境,推荐使用appserv,下载链接 链接: http://pan.baidu.com/s/1cHqSOY 密码: b8w2 2.安装redis 下载链接 链接: http://pa ...
- 【SEO】搜索引擎优化的陷阱和作弊
一.认识SEO [理解] 站内优化是指更改网站内部结构,让网站利于蜘蛛爬取,比如网站内容: 站外优化是指发反向链接,给蜘蛛一个爬取你网站的通道. 其中,反向链接是指网页A 上有一个链接指向网页B,则网 ...
- vijos1051题解
题目: 圣诞老人回到了北极圣诞区,已经快到12点了.也就是说极光表演要开始了.这里的极光不是极地特有的自然极光景象.而是圣诞老人主持的人造极光. 轰隆隆--烟花响起(来自中国的浏阳花炮之乡).接下来就 ...
- input file 上传图片问题
html代码如下: <input id="fileup" type="file" accept="image/*" capture=& ...
- canvas一周一练 -- canvas基础学习
从上个星期开始,耳朵就一直在生病,里面长了个疙瘩,肿的一碰就疼,不能吃饭不能嗨 (┳_┳)……在此提醒各位小伙伴,最近天气炎热,一定要注意防暑上火,病来如山倒呀~ 接下来我正在喝着5块一颗的药学习ca ...
- JS事件响应的学习总结
点击事件:ocnlick 鼠标经过事件:onmouseover 鼠标移开事件:onmouseout 获得焦点事件:onfocus 失去焦点事件:onblur 内容选中事件:onselect 文本框内容 ...
- 腾讯云centos7服务器环境搭建,tomcat+jdk+mysql+nginx
软件:jdk 1.8.0_45 tomcat 8.5.8 mysql 5.6.36 nginx 1.10.x或以上 其中tomcat在centos6.8中没问题,centos7中会出现卡在启动那里 I ...
- AddBinary
二进制加法 输入2个字符串,字符串内由0和1组成:计算二者之和,返回字符串 Given two binary strings, return their sum (also a binary stri ...
- 华为OJ之最长公共子序列
题目描述: 对于两个给定的字符串,给出他们的最长公共子序列. 题目分析: 1,在之前的博文(http://www.cnblogs.com/yonguo123/p/6711360.html)中我们讨论了 ...
- jmeter问题处理随笔1 - 自动遍历用例(一次)
背景: 弄了个无业务关联的巡检接口方案,需要循环获取csv中的数据,每一条数据,运行一次,直到全部运行完,但是使用后发现要通过[线程组]中设置循环变量的数据为来读取csv中的数据,这样每次修改csv用 ...