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的一个简化吧,主要也是使用两个指针向中间遍历的方法。代码如下:

public class Solution {
public int threeSumClosest(int[] nums, int target) {
int len = nums.length;
int ans = 0;
boolean bans = false;
Arrays.sort(nums);
for(int i=0;i<len-1;i++){
if(i>0 && nums[i] == nums[i-1])
continue;
int left = i+1,right = len-1;
while(left<right){
int sum = nums[i]+nums[left]+nums[right];
if(sum == target){
return ans = sum;
}
else if( sum > target ){
right--;
}
else{
left++;
}
if(!bans){
ans = sum;
bans = true;
}
else if( Math.abs(target-ans) > Math.abs(target-sum) ){
ans = sum;
}
}
}
return ans;
}
}
 

Leetcode Array 16 3Sum Closest的更多相关文章

  1. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  2. 《LeetBook》leetcode题解(16):3Sum Closest [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  4. 【一天一道LeetCode】#16. 3Sum Closest

    一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...

  5. 【LeetCode】16. 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  6. LeetCode:16. 3Sum Closest(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...

  7. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

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

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

  9. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

随机推荐

  1. python的get和post

    postimport urlliimport urllib #post数据value = {}value['username']='aaaa'value['password']='123123'dat ...

  2. shell变量的数值计算

    shell中常见的算术运算命令如下 1.(())  用于整数运算的常用运算符,效率很高 2.let 用于整数运算,类似于  (()) 3.expr  可用于整数计算,但还有很多其他的额外功能 4.bc ...

  3. 2017-2018-2 20179204 PYTHON黑帽子 黑客与渗透测试编程之道

    python代码见码云:20179204_gege 参考博客Python黑帽子--黑客与渗透测试编程之道.关于<Python黑帽子:黑客与渗透测试编程之道>的学习笔记 第2章 网络基础 t ...

  4. O(1)gcd学习笔记

    设最大权值为\(M\) \(T=\sqrt M\) 定理 任意一个\(\le M\)的数一定可以表示为abc三个数的乘积 满足这三个数要么\(\le T\),要么是一个质数 证明: 考虑反证 假设\( ...

  5. BZOJ 3876 支线剧情

    支线剧情 [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩怨情仇的剧情.这些游戏往往都有很多的支线剧情,现在JYY想花费最 ...

  6. [LeetCode] 4Sum hash表

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  7. Linux signal那些事儿【转】

    转自:http://blog.chinaunix.net/uid-24774106-id-4061386.html Linux编程,信号是一个让人爱恨交加又不得不提的一个领域.最近我集中学习了Linu ...

  8. class.getDeclaredFields()与class.getFields()

    * getFields()与getDeclaredFields()区别:getFields()只能访问类中声明为公有的字段,私有的字段它无法访问.getDeclaredFields()能访问类中所有的 ...

  9. php --图片加图片水印

    最近在做一个视频网站需要视频有一个封面图片,但是不能是普通的图片,能让别人一眼看出来是 视频,所以我就在图片上面加了视频播放器的那种水印,具体代码如下: <?php/** * 图片加水印(适用于 ...

  10. 方程式组织EQUATION DRUG平台解析(提纲) —方程式组织系列分析报告之四

    https://www.bleepingcomputer.com/news/security/shadow-brokers-release-new-files-revealing-windows-ex ...