Question:

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).

Analysis:

与3Sum题目比较起来,这里不需要去重检测,只需要有两个变量控制sum和与target间的差距即可。总体的策略同3Sum。

Answer:

public class Solution {
public int threeSumClosest(int[] nums, int target) {
int tar = Integer.MAX_VALUE;
int res = 0;
Arrays.sort(nums); for(int i=0; i<nums.length-2; i++) {
int j = i + 1;
int k = nums.length - 1; while(j < k) {
int sum = nums[i] + nums[j] + nums[k];
if(sum == target)
return sum;
else {
int dif = Math.abs(sum - target);
if(dif < tar) {
tar = dif;
res = sum;
}
if(sum < target)
j++;
else
k--;
}
}
}
return res;
}
}

LeetCode -- 3SumCloset的更多相关文章

  1. 2016/10/28 很久没更了 leetcode解题 3sumcloset

    16.3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closes ...

  2. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  3. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  4. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  8. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  9. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

随机推荐

  1. yum仓库客户端搭建和NTP时间同步客户端配置

    一.yum仓库客户端搭建 yum源仓库搭建分为服务器端和客户端. 服务端主要提供软件(rpm包)和yumlist.也就是提供yum源的位置.一般是通过http或者ftp提供位置. 客户端的配置:yum ...

  2. Java : java基础(2) 集合&正则&异常&File类

    Obj 方法: hashCode() 返回内存地址值, getClass() 返回的时运行时类, getName() 返回类名, toString() 把名字和hashCode() 合在一起返回,如果 ...

  3. Delphi初始化与结束化

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  4. narcissus

    public class narcissus { public static void main(String args[]) { long u=0,t=0,h=0,y=0,k=0; for(long ...

  5. 001---Python简介

    编程语言: 机器语言 最底层,更容易被计算机识别,执行速度最快 复杂,开发效率低 汇编语言 比较底层,执行速度较快 同样复杂 高级语言 编译型语言:先编译,后执行.生成独立的可执行文件.是计算机可以理 ...

  6. kafka topic 完全删除

    kafka topic 完全删除   1.自动删除脚本(得配置server.properties 中 delete.topic.enable=true) ./kafka-topics.sh --zoo ...

  7. Android Stadio 指定文件打开类型

    我们项目里面,有一个文件,叫做aaa.meta. 这个只是一个配置文件,里面是txt. 但是Android Stadio 不识别.怎么办? 设置如下图: 首先,打开Android stadio 的设置 ...

  8. android staido 断点遇到的坑

    今天排查数据布点问题,发现sd卡上面的文件莫名消失. 怎么可能?系统不可能删除你的文件,但是我调试,删除文件的代码, 一直都没有执行啊. 后来发现,子线程里面代码,android stadio 可能断 ...

  9. Android当代码方法超过65536个时,在2.3机器上会不能安装,出现INSTALL_FAILED_DEXOPT错误

    今天打包时,发现2.3机器,产生的APK在某些机器上不能安装(Installation error: INSTALL_FAILED_DEXOPT),针对这个问题的一个可能解释是:最新的ADT和SDK ...

  10. Hadoop2.5.2集群部署(完全分布式)

    环境介绍 硬件环境 CPU 4 MEM 4G 磁盘 60G  软件环境 OS:centos6.5版本 64位 Hadoop:hadoop2.5.2 64位 JDK: JDK 1.8.0_91 主机配置 ...