public class Solution {
public int threeSumClosest(int[] num, int target) {
int result = num[0] + num[1] + num[num.length - 1];
Arrays.sort(num);
for (int i = 0; i < num.length - 2; i++) {
int start = i + 1, end = num.length - 1;
while (start < end) {
int sum = num[i] + num[start] + num[end];
if (sum > target) {
end--;
} else {
start++;
}
if (Math.abs(sum - target) < Math.abs(result - target)) {
result = sum;
}
}
}
return result;
}
}

[Leetcode]016. 3Sum Closest的更多相关文章

  1. [Leetcode][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

  2. 【JAVA、C++】LeetCode 016 3Sum Closest

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

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

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

  4. No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  5. 【leetcode】3Sum Closest

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  6. LeetCode--No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

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

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

  8. 【LeetCode】016 3Sum Closest

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

  9. LeetCode (13): 3Sum Closest

    https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...

随机推荐

  1. solr4.8中集成mmseg4j1.9.1

    要想在Solr中整合mmseg4j其实很容易,只需要如下几个步骤 1.下载(https://code.google.com/p/mmseg4j/downloads/list)并解压mmseg4j-1. ...

  2. linq to sql 不能更新的问题

    今天在项目中用linq更新一个表的时候,结果怎么都更新不了,最蛋疼的是什么异常也不报,发现db.table1.isReadOnly为True 知道问题所在,百度后得到解决办法: 原来是我的表没有增加主 ...

  3. Vue02 样式的动态绑定

    daigengxin......2018-3-8 21:09:18 跟angular2类似,分为CSS类绑定和Style样式绑定两种方式,详情参见

  4. 204. Count Primes 统计<n的质数的个数

    [抄题]: Count the number of prime numbers less than a non-negative number, n. [暴力解法]: 时间分析: 空间分析: [优化后 ...

  5. Windows 安装 Microsoft Visual Studio 2010

    Windows 安装 VS2010 我的电脑系统:Windows 10 x64位 我要安装的Microsoft Visual Studio 是:VS2010 注意: VS2010 没有专门的64位版. ...

  6. c++ vitual继承

    为了解决多继承下的二义性而设计的vitrul继承 class Base { public: Base(void); ~Base(void); }; Base::Base(void) { printf( ...

  7. (树)判断二叉树是否为BST

    题目:判断一颗二叉树是否为BST. 思路:其实这个问题可以有多个解决方法. 方法一:递归解决.根据BST的特性.左边的小于根节点的值,右边的大于根节点的值.并且对于每一棵子树都是如此.所以我们可以直接 ...

  8. Jmeter-BeanShell的使用介绍

    最近学习使用了jmeter来对接口进行测试.使用jmter进行接口测试,有时候需要编写一些BeanShell脚本语言,或者利用BeanShell调用自己的工具类,来完成jmeter基本功能中无法实现的 ...

  9. 基于DEV控件库的webservice打印.repx模板

    本文使用的DEV版本为10.1版本 首先需要添加Dll引用 DevExpress.Data.v10.1 DevExpress.XtraPrinting.v10.1 DevExpress.XtraRep ...

  10. File类和Directory类

    File类和Directory类分别用来对文件和各种目录进行操作,这两类可以被实例化,但不能被其他类集成. 1. File类(静态类) File类支持对文件的基本操作,它包括用于创建.复制.删除.移动 ...