题目: https://leetcode.com/problems/3sum-closest/

【标签】Array; Two Pointers

【个人分析】

  这道题和它的姊妹题 3Sum 非常类似, 就不再多说了,具体一些的分析可以参考 [Leetcode][015] 3Sum

 public class Solution {
public int threeSumClosest(int[] nums, int target) {
int result = target;
int len = nums.length;
if (len < 3) {
return 0;
}
Arrays.sort(nums);
for (int i = 0; i < len; i++) {
int number = nums[i]; int leftIndex = i + 1;
int rightIndex = len - 1;
while (leftIndex < rightIndex) {
int threeSum = number + nums[leftIndex] + nums[rightIndex];
if (threeSum == target) {
// best result found!
return target;
} else {
// update global result
if ( result == target ||
Math.abs(target - threeSum) < Math.abs(target - result)) {
result = threeSum;
}
if (threeSum < target) {
while (leftIndex < rightIndex &&
nums[leftIndex] == nums[leftIndex + 1]) {
leftIndex++;
}
leftIndex++;
} else {
while (leftIndex < rightIndex &&
nums[rightIndex] == nums[rightIndex - 1]) {
rightIndex--;
}
rightIndex--;
}
}
}
}
return result;
} }

[Leetcode][016] 3Sum Closest (Java)的更多相关文章

  1. 【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 ...

  2. leetcode 16. 3Sum Closest JAVA

    题目: 给定一个包括n个整数的数组nums和一个目标值target.找到nums中的三个整数,使得他们之和与target最为接近.返回三个整数之和,假定每组输入只存在唯一答案 解题思路: 将nums数 ...

  3. [Leetcode]016. 3Sum Closest

    public class Solution { public int threeSumClosest(int[] num, int target) { int result = num[0] + nu ...

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

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

  5. No.016 3Sum Closest

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

  6. 【leetcode】3Sum Closest

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

  7. LeetCode--No.016 3Sum Closest

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

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

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

  9. Java [leetcode 16] 3Sum Closest

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

随机推荐

  1. 一个php小白找工作的历程

    一个php小白找工作的历程其实对新工作还是有点忐忑的,对于我这样一个有着特殊工作经历的来说更是如此.为了更好的迎接未来,不得不总结下过去.在经历一段时间的职业生涯探索期后,还是觉得自己更适合做程序员这 ...

  2. Python自动化运维之7、生成器、迭代器、列表解析、迭代器表达式

    迭代器和生成器 1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外 ...

  3. error on line 1 at column 6: XML declaration allowed only at the start of the document

    This page contains the following errors: error on line 1 at column 6: XML declaration allowed only a ...

  4. 判断iOS设备是否越狱

    - (BOOL)isJailbroken { BOOL jailbroken = NO; NSString *cydiaPath = @"/Applications/Cydia.app&qu ...

  5. d3可视化实战03:神奇的superformula

    需求驱动实现 前文讲过了D3的数据驱动机制,中间所举的例子都很简单.例如那个demo里面,绑定的数据是一个简单的数组,实现的图元也仅仅是一堆用SVG画的circle.但是现实世界中我们往往会遇到复杂的 ...

  6. How systems researchers build systems

    Define the problem >>Identify the constraints and abstract problem propose solution:simple ide ...

  7. Altium Designer如何批量修改名称,数值,封装

    方法一: altium里的封装管理库 1,Tools -> Footprint Manager -> ...2,在Component List里选择要改的器件3,在View and Edi ...

  8. Java基础加强学习笔记(二)

    一.反射的基础Class类 1.如何得到各个字节码对应的实例对象 (1)类名.class,例如 System.class (2)对象.getClass(),例如 new Data().getClass ...

  9. Linux系统编程(36)—— socket编程之UDP详解

    UDP 是User DatagramProtocol的简称,中文名是用户数据报协议.UDP协议不面向连接,也不保证传输的可靠性,例如: 1.发送端的UDP协议层只管把应用层传来的数据封装成段交给IP协 ...

  10. 【转】secureCRT使用退格键(backspace)出现^H解决办法

    原文网址:http://skykiss.blog.51cto.com/blog/2892603/769771 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将 ...