题目

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. Android--------TabLayout实现新闻客户端顶部导航栏

    APP市场中大多数新闻App都有导航菜单,导航菜单是一组标签的集合,在新闻客户端中,每个标签标示一个新闻类别,对应下面ViewPager控件的一个分页面,今日头条, 网易新闻等. 随着版本迭代的更新, ...

  2. Spark项目之电商用户行为分析大数据平台之(九)表的设计

    一.概述 数据设计,往往包含两个环节: 第一个:就是我们的上游数据,就是数据调研环节看到的项目基于的基础数据,是否要针对其开发一些Hive ETL,对数据进行进一步的处理和转换,从而让我们能够更加方便 ...

  3. linux 的常用命令---------第四阶段

    权限管理 “4” “r” → 读权限: 查看文件内容: 是否能够列出目录结构. “2” “w” → 写权限: 编辑文件内容: 是否能够创建.删除.复制.移动目录. “1” “x” → 执行权限: 对二 ...

  4. oracle版本兼容问题

    问题一描述:本机环境升级为vs2012升级TLS程序为framework4.0,本机ODAC为ODTwithODAC112030.本机为oracle10g本机程序生成成功,运行成功. 发布到服务器后, ...

  5. python_基础语法

    开始正式接触python的语法: 1. 2.

  6. 20155222卢梓杰 实验一 逆向及Bof基础

    实验一 逆向及Bof基础 1.实验对象为32位可执行文件pwn1,这个程序主要有main.foo.getshell这三个函数,其中foo函数功能为输出输入的字符串,getshell函数功能为打开一个s ...

  7. 2017-2018-2 20155315《网络对抗技术》Exp9 :Web安全基础

    实验目的 理解常用网络攻击技术的基本原理. 教程1 教程2 教程3 实验内容 SQL注入攻击 XSS攻击 CSRF攻击 Webgoat前期准备 从GitHub上下载jar包 拷贝到本地,并使用命令ja ...

  8. # 20155337《网络对抗》Exp7 网络欺诈防范

    20155337<网络对抗>Exp7 网络欺诈防范 实践目标 实践内容 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法.具体实践有 (1)简单应用SET工具建立 ...

  9. Oracle中,如何查看FRA(Flashback Recovery Area)的利用率

    例子: SQL> set linesize 300SQL> select * from V$RECOVERY_AREA_USAGE; FILE_TYPE PERCENT_SPACE_USE ...

  10. 汇编 ADD指令

    知识点: 加法汇编指令ADD 一.加法指令 ADD(Addition) 格式 格式: ADD A,B //A=A+B; 功能: 两数相加 . OPRD1为任一通用寄存器或存储器操作数,可以是任意一个 ...