题目: 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. phpcms v9 表单向导里添加图片字段,提示会话过期,请重新登陆

    用 phpcms v9 制作前端用户提交信息时,有个图片上传的栏目. 如果注释 /phpcms/modules/attachment/attachment.php 20//判断是否登录 21 if(e ...

  2. Swift—静态方法-备

    静态方法与静态属性类似,Swift中定义了静态方法,也称为类型方法.静态方法的定义与静态属性类似,枚举和结构体的静态方法使用的关键字是static:类静态方法使用的关键字是class或static,如 ...

  3. Summation of primes

    是我算法不对,还是笔记本CPU太差? 我优化了两次,还是花了三四个小时来得到结果. 在输出上加1就是最终结果. The sum of the primes below 10 is 2 + 3 + 5 ...

  4. Android开发程序获取GPS信息步骤

    1.获取LOCATION_SERVICE系统服务.2.创建Criteria对象,调用该对象的set方法设置查询条件.3.调用LocationManager.getBestProvider(Criter ...

  5. 子句判断、启动强度和去模糊化--AForge.NET框架的使用(三)

    原文:子句判断.启动强度和去模糊化--AForge.NET框架的使用(三) 使用AForge.NET进行模糊运算 上一篇说来一些模糊运算的数学问题,用AForge.NET做相关运算就很简单了. 1.联 ...

  6. Oracle实例与Oracle数据库

    数据库实例是用于和操作系统进行联系的标识,也就是说数据库和操作系统之间的交互使用的是数据库实例. 数据库实例名(instance_name) 实例名也被写入参数文件中,该参数为instance_nam ...

  7. Boost 和 STL 相比有哪些优势和劣势?

    1. 在设计原则上,STL和Boost大体统一因为STL和Boost基本上都是标准委员会那批人在策划.审核和维护,所以口味上是相对接近的.但是因为Boost并不在标准中,或者说是下一代标准的试验场,所 ...

  8. Python socket 客户端和服务器端

    connection, address = socket.accept() 调 用accept方法时,socket会时入“waiting”状态.客户请求连接时,方法建立连接并返回服务器.accept方 ...

  9. ubuntu配置bridge网桥

    先安装uml-utilities,该工具包含建立虚拟网络设备(所谓的“TAP interfaces”)的工具: sudo apt-get install uml-utilities 安装 桥接工具 b ...

  10. Shiro-授权

    把 realms 配置给SecurityManager 在认证的时候单个realm是这样配置的: <bean id="securityManager" class=" ...