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. 洛谷P1822 魔法指纹 【分块打表】

    题目 对于任意一个至少两位的正整数n,按如下方式定义magic(n):将n按十进制顺序写下来,依次对相邻两个数写下差的绝对值.这样,得到了一个新数,去掉前导0,则定义为magic(n).若n为一位数, ...

  2. apache 列目录修复

    描述   :Apache默认配置时允许目录浏览.如果目录下没有索引文件,则会出现目录浏览,导致文件信息泄漏. 检测方法:直接访问目录,如果能看到目录下的文件信息,则说明存在目录浏览漏洞 加固方法: 1 ...

  3. cxGrid让指定的某行自动呈选选中的状态

    cxView.ViewData.Rows[cxView.DataController.DataSource.DataSet.RecNo-1].Selected := True;//将当前的行呈选中的状 ...

  4. 洛谷 P1270 “访问”美术馆

    P1270 “访问”美术馆 题目描述 经过数月的精心准备,Peer Brelstet,一个出了名的盗画者,准备开始他的下一个行动.艺术馆的结构,每条走廊要么分叉为两条走廊,要么通向一个展览室.Peer ...

  5. MSClass (Class Of Marquee Scroll通用不间断滚动JS封装类) Ver 1.65

    原文发布时间为:2010-02-07 -- 来源于本人的百度文章 [由搬家工具导入] http://www.popub.net/script/MSClass.html/*MSClass (Class ...

  6. 写文章 TEE技术分析【转】

    转自:https://zhuanlan.zhihu.com/p/24222064 首先介绍一下TEE的主要关键技术: 1.安全启动(Secure Boot) 安全启动技术可以用于需要防止篡改系统镜像, ...

  7. Scrapy学习-23-分布式爬虫

    scrapy-redis分布式爬虫 分布式需要解决的问题 request队列集中管理 去重集中管理 存储管理   使用scrapy-redis实现分布式爬虫 github开源项目: https://g ...

  8. AC日记——青蛙的约会 poj 1061

    青蛙的约会 POJ - 1061   思路: 扩展欧几里得: 设青蛙们要跳k步,我们可以得出式子 m*k+a≡n*k+b(mod l) 式子变形得到 m*k+a-n*k-b=t*l (m-n)*k-t ...

  9. 调用Thread.interrupt()方法到底会发生什么?

    1. 当线程处于Blocked状态(sleep,wait,join),线程会退出阻塞状态,并抛出一个InterruptedException.park除外,它有响应但是不会抛出异常 2. 当线程处于R ...

  10. Scaling the Messages Application Back End 【转】

    11年的blog. Facebook Messages seamlessly integrates many communication channels: email, SMS, Facebook ...