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. libc++abi.dylib`__cxa_throw: 使用[AVAudioPlayer play]会产生__cxa_throw异常

    libc++abi.dylib`__cxa_throw: 使用[AVAudioPlayer play]会产生__cxa_throw异常 开发中遇到一个奇怪的异常.我调用AVAudioPlayer pl ...

  2. git常见操作总结

    git config --global user.name "liyang"git config --global user.email "liyangslj@126.c ...

  3. 用Razor来生成模板 using razor for template

    原文发布时间为:2011-09-15 -- 来源于本人的百度文章 [由搬家工具导入] http://razorengine.codeplex.com/

  4. food(洛谷P4040 [AHOI2014/JSOI2014]宅男计划)

    题目在这里 题目描述 外卖店一共有N种食物,分别有1到N编号.第i种食物有固定的价钱Pi和保质期Si.第i种食物会在Si天后过期.JYY是不会吃过期食物的. 比如JYY如果今天点了一份保质期为1天的食 ...

  5. 复制View对象

    
Mark一下 - (UIView*)duplicate:(UIView*)view { NSData * tempArchive = [NSKeyedArchiver archivedDataWit ...

  6. C#将图片进行马赛克处理

    /// <summary> /// 马赛克处理 /// </summary> /// <param name="bitmap"></par ...

  7. 【C/C++】快速排序的两种实现思路

    方法一:不断填坑,一次确定一个值.http://blog.csdn.net/morewindows/article/details/6684558 #include<stdio.h> vo ...

  8. hdu 4738 无向图缩点断桥 // 细节坑题

    Caocao's Bridges 题意:给个无向图,求出边权最小的桥. 一看,直接缩点,若无桥,输出-1,有桥,遍历下边,更新最小..分分钟搞定,以为IA的..一交wa... 坑点:1:若原图不连通, ...

  9. 安卓edittext实现输入数字限制条件的效果

    我们知道edittext能指定输入字符类型,这次我们就来了解下在数字模式下的一些显示控制输入的效果 1.限制输入数字 android:inputType="number|numberDeci ...

  10. Educational Codeforces Round 35 A. Nearest Minimums【预处理】

    [题目链接]: Educational Codeforces Round 35 (Rated for Div. 2) A. Nearest Minimums time limit per test 2 ...