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. <!doctype html>这个是干什么的???

    html5标准网页声明,原先的是一串很长的字符串,现在是这个简洁形式,支持html5标准的主流浏览器都认识这个声明.表示网页采用html5

  2. [patl2-001]紧急救援

    解题关键:最短路的变形. 1.按顶点存储,$O(n^2)$ #include<cstdio> #include<cstring> #include<algorithm&g ...

  3. IDEA01 创建java项目、创建web项目

    注意:本教程使用的开发环境是:(专业版) 1 创建javaSE项目 1.1 file -> new -> project 注意:如果是第一次使用,就需要配置 project SDK  , ...

  4. Condition实现多个生产者多个消费者

    Condition实现多对多交替打印: import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.R ...

  5. mysql 5.7.11 源码安装

    mysql5.711安装 1.安装boost包下载地址http://sourceforge.net/projects/boost/files/boost/ 2.解压boost_1_59_0.tar.g ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  7. Luogu 2151 [SDOI2009]HH去散步

    BZOJ 1875 矩阵乘法加速递推. 如果不要求不能走同一条边,那么直接构造出矩阵快速幂即可,但是不走相同的道路,怎么办? 发现边数$m$也很小,我们直接把$2 * m$开成一个矩阵,相当于记录上一 ...

  8. 网络笔记-unity 实现AOP

    该文章来自网络,如有冒犯,请及时联系! 前提 引用以下文件 Microsoft.Practices.ObjectBuilder2.dll Microsoft.Practices.Unity.dll M ...

  9. SDUT 3377 数据结构实验之查找五:平方之哈希表

    数据结构实验之查找五:平方之哈希表 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 给定的一组 ...

  10. SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)

    求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description  已知一 ...