[算法题] 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 ...
随机推荐
- tab切换实现方式2
tab切换实现方式2: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- 什么是VPN,VPN有什么用,怎么获得VPN
什么是VPN? VPN英文全称是“Virtual Private Network”,翻译过来就是“虚拟专用网络”.vpn被定义为通过一个公用网络(通常是因特网)建立一个临时的.安全的连接,是一条穿过混 ...
- JQuery——banner旋转木马效果
博主在浏览网页时无意间发现了一种banner图的轮播方式--像旋转木马一样的轮播方式,博主感觉非常新颖独特,经过查阅资料,观看某课网教程总算搞了出来的,其原理主要利用了JQuery代码实现,好了不多说 ...
- maven自定义jar到本地仓库
Apache Maven为项目构建提供了绝佳的解决方案,其本地仓库中缓存了远程代理仓库或中央仓库中的资源,从而提高网络资源使用效率,很好很强大! 但是并非所有资源都可以根据GroupId.Artif ...
- 【SEO】搜索引擎优化的陷阱和作弊
一.认识SEO [理解] 站内优化是指更改网站内部结构,让网站利于蜘蛛爬取,比如网站内容: 站外优化是指发反向链接,给蜘蛛一个爬取你网站的通道. 其中,反向链接是指网页A 上有一个链接指向网页B,则网 ...
- AddNewsServlet -servlet处理响应请求
package com.pb.news.web.servlet; import java.io.File;import java.io.IOException;import java.util.Dat ...
- biz-NewsService
package com.pb.news.service; import java.util.List; import com.pb.news.entity.News; public interface ...
- ARP欺骗分析
(作者原创,欲转载请说明出处)1.arp介绍 arp:地址解析协议;将IP地址映射为MAC地址.2.为什么要有arp 平时上网我们都知道要有一个IP地址才能上网,那arp用来干嘛的呢?如果 ...
- Memcached在windows下的基本使用
1.Memcached是什么 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动 ...
- 设备常用框架framework
framework名称 framework说明 framework文档 Accelerate.framework 包含加速数学和DSP函数 http://developer.apple.com/iph ...