题目内容

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的更多相关文章

  1. 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 ...

  2. [算法题] 3Sum

    题目内容 题目来源:LeetCode Given an array S of n integers, are there elements a, b, c in S such that a + b + ...

  3. 算法(6)3Sum Closest

    kSum问题是一类问题,基本的方法是两个循环,其他一个查找,但是今天碰到了一个超级棘手的问题,是3Sum问题的一个变型 问题:给定一个数组,给定一个整数k,要求找出在数组中找到3个整数,使得这三个整数 ...

  4. 算法题丨3Sum

    描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  5. LeetCode算法题-Maximize Distance to Closest Person(Java实现)

    这是悦乐书的第328次更新,第351篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第198题(顺位题号是849).在一排座位中,1表示一个人坐在该座位上,0表示座位是空的 ...

  6. 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 ...

  7. 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 ...

  8. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  9. 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 ...

随机推荐

  1. PHP xmapp 下面安装 Composer-Setup.exe

    1.打开PHP配置文件E:\xampp\php\php.ini确认以下模块已开启(移除前面的分号). extension=php_openssl.dll, (php.ini文档里面开启一次就OK了) ...

  2. 基于REM的移动端响应式适配方案

    视口 在前一段时间,我曾经写过一篇关于viewport的文章.最近由于在接触移动端开发,对viewport有了新的理解.于是,打算重新写一篇文章,介绍移动端视口的相关概念. 关于这篇文章说到的所有知识 ...

  3. peoplesoft function PSTREENODE 通过 deptid 获得部门树 一级部门 code

    create or replace function ht_gettopdeptid(deptid in varchar) return varchar2 is r ); c int; m ); r_ ...

  4. (转载)基于Bash命令行的百度云上传下载工具

    原文链接:http://hi.baidu.com/meoow/item/aef5814bbd5be3e1bcf451e9 这是我根据百度云PCS的API写的一个基于bash的命令行工具, 使用了cur ...

  5. React环境配置

    现在开始配置一个基础项目. 创建项目文件夹:C:\Users\Danny\Desktop\React npm init 创建package.json文件 下面的所有安装,都是--save-dev,因为 ...

  6. html之结构化标记

    1.什么是结构化标记 对布局使用的div 进行升级 , 根据页面的不同区域而提供的不同标签.作用与div几乎一致 专门用于搭建网站结构而用 2.结构标记详解 1.<header>元素 作用 ...

  7. 在react.js上使用antd-design没有样式

    两种解决方法: 第一种: 在.babelrc中加入 { "presets": ["es2015", "react"], "plug ...

  8. POJ 2152 fire / SCU 2977 fire(树型动态规划)

    POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...

  9. Andorid源码系列:View的onTouchEvent()与performClick(),performLongClick()调用时机解析

    这是大土豆的第一篇博客,想着工作3年多了,在工作上从一名菜鸟逐渐成长为在项目中能干点事的人,自己对Android的见解也一步步加深,有必要写一些对Android代码和开发过程中的感悟,和广大朋友们分享 ...

  10. 事件冒泡、事件委托、jQuery元素节点操作、滚轮事件与函数节流

    一.事件冒泡定义 事件冒泡是指在一个对象触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那 ...