题目

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. [题目] Luogu P3716 [CTSC2000]冰原探险

    题面 题目背景 传说中,南极有一片广阔的冰原,在冰原下藏有史前文明的遗址.整个冰原被横竖划分成了很多个大小相等的方格.在这个冰原上有N个大小不等的矩形冰山,这些巨大的冰山有着和南极一样古老的历史,每个 ...

  2. Docker技术入门与实战 第二版-学习笔记-9-Docker Compose 项目-1-举例说明

    Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责快速在集群中部署分布式应用 Compose 通过一个配置文件来管理多个Docker容器,在配置文件中 ...

  3. find和find_if

    find函数 是在一个迭代器范围内查找特定元素得函数,可将将他用于任意容器类型得元素.这个函数返回的是所找元素得引用,如果没有找到元素就会返回这个容器得尾迭代器. #include <iostr ...

  4. [转]VS2013+简单稀疏光束调整库SSBA配置(64位编译)

    有关SSBA库的资源比较少,我是在Github上搜索下载的,具体的GitHub官方下载地址为:SSBA 下载后在SSBA解压文件夹下新建文件夹build. 打开cmake gui,在source co ...

  5. 通过SSH秘钥登录线上MySQL数据库(基于Navicat)

    前言 生产环境的数据库往往需要经过严格的安全限制,所以禁用密码登录,使用秘钥的方式是一种相对安全的登录方式. 原理: 角色: 主机A:其他主机,有访问线上数据库的权限 主机B:线上数据库的主机 主机C ...

  6. Block abstraction view(Create & Reference)

    在hierarchical design 中,一般需要调用 hard macro,top调用 macro 的方法有多种: 1. 调用macro对应的db 2. 调用 macro 的 ilm 模型(20 ...

  7. 浅谈 MVC 和 MTV

    浅谈 MVC 和 MTV 一.MVC M:model,模型,就是数据模型,负责数据的存取: V:view,视图,负责页面的展示逻辑: C:controller,控制器,负责业务逻辑的处理: 二.MTV ...

  8. 基于Python自动上传包到nexus仓库

    1.设计思路 用户通过excel表格的形式填写包的信息,并将包一起发送给负责人 2.代码实现 #coding:utf8 import os import xlrd def GetData(fileNa ...

  9. 【LeetCode5】Longest Palindromic Substring★★

    1.题目描述: 2.解题思路: 题意:求一个字符串的最长回文子串. 方法一:中心扩展法.遍历字符串的每一个字符,如果存在回文子串,那么中心是某一个字符(奇数)或两个字符的空隙(偶数),然后分两种情况( ...

  10. git reset之后找回本地未提交的代码

    头脑发热使用了git reset命令回退到了之前的一个版本,结果把本地没有提交的代码给覆盖掉了..... 作为一个bug员自然是想恢复,毕竟重新写还得再测一遍,本着能懒一点是一点的原则,开始了恢复代码 ...