题目

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

翻译

和 3sum 那道题大同小异... 传送门

只不过这次不再是求和为0的三个数也不是返回所有解,而是求出离target最近的三个数的和并返回

Hints

Related Topics: Array, Two Pointers

代码

Java

class Solution {
public int threeSumClosest(int[] nums, int target) {
int result = nums[0]+nums[1]+nums[nums.length-1];
Arrays.sort(nums);
for(int i=0;i<nums.length-2;i++){
int l = i+1, r = nums.length-1;
while(l<r){
int s = nums[i]+nums[l]+nums[r];
if(s>target)
r--;
else
l++;
if(Math.abs(target-s)<Math.abs(target-result))
result = s;
}
}
return result;
}
}

Python

class Solution(object):
def threeSumClosest(self, nums, target):
nums.sort()
closest = nums[0]+nums[1]+nums[len(nums)-1]
if len(nums)<3: return for i in range(0,len(nums)-2):
l, r = i+1, len(nums)-1
while l<r:
s = nums[i]+nums[l]+nums[r]
if abs(target-s)<=abs(target-closest):
closest = s
if s<target:
l+=1
elif s>target:
r-=1
else:
return target
return closest

蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]的更多相关文章

  1. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  2. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  3. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]

    题目 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 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  6. 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  7. 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]

    题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...

  8. 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]

    题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  9. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

随机推荐

  1. BZOJ5092:[Lydsy1711月赛]分割序列(贪心,高维前缀和)

    Description 对于一个长度为n的非负整数序列b_1,b_2,...,b_n,定义这个序列的能量为:f(b)=max{i=0,1,...,n}((b_1 xor b_2 xor...xor b ...

  2. css 文本溢出

    多行文本溢出处理: display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; // 3 行 overflow ...

  3. linux ssh 应用

    linux 服务器 连接另一个linux服务器 ssh 用户名@IP地址 linux 服务器传输文件到另一个linux服务器 scp 文件名(可多个)  用户名@IP地址:传到的目录 /home

  4. oracle kill 锁

    select object_name as 对象名称,s.sid,s.serial#,p.spid as 系统进程号 from v$locked_object l , dba_objects o , ...

  5. Underscore.js 入门

    Underscore封装了常用的JavaScript对象操作方法,用于提高开发效率.将它单独运用到任何一个页面.(另外,Underscore还可以被使用在Node.js运行环境.) 在学习Unders ...

  6. (转)Centos7 修改硬件时间和系统时间

    查看硬件时间 [root@localhost ~]# hwclock --show Tue 13 Jun 2017 02:11:12 AM CST -0.848845 seconds 1 2 可以看出 ...

  7. #HTTP协议学习# (一)request 和response 解析

    注:本文转自:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html , 粉字[]内内容为个人笔记 当今web程序的开发技术真是 ...

  8. 关于原子哥ENC28J60网络通信模块接收数据代码的一点疑惑

    ---恢复内容开始--- 这几天做STM32的ENC28J60网络通信模块,自己在原子哥的代码上进行修改测试,,发现一个问题,电脑和板子进行通信的时候总隔一段时间板子就死机了. 使用自己的就不会死机, ...

  9. Liunx-mkdir命令

    1. 新建一个文件夹 one 2. 新建三个文件夹three,four,five 3. 新建一个多层级文件夹 201904/a/01

  10. iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK

    iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言:    今天给大家 ...