[Leetcode][016] 3Sum Closest (Java)
题目: 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)的更多相关文章
- 【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 ...
- leetcode 16. 3Sum Closest JAVA
题目: 给定一个包括n个整数的数组nums和一个目标值target.找到nums中的三个整数,使得他们之和与target最为接近.返回三个整数之和,假定每组输入只存在唯一答案 解题思路: 将nums数 ...
- [Leetcode]016. 3Sum Closest
public class Solution { public int threeSumClosest(int[] num, int target) { int result = num[0] + nu ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- LeetCode--No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- 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 ...
随机推荐
- Asp.net core与golang web简单对比测试
最近因为工作需要接触了go语言,又恰好asp.net core发布RC2,就想简单做个对比测试. 下面是测试环境: CPU:E3-1230 v2 内存:16G 电脑有点不给力 操作系统:Centos7 ...
- codevs 1068 乌龟棋
题目描述 Description 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一 的起点,第N格是终点,游戏要求玩家控制 ...
- CCAN:C语言的模块仓库
实践中一门编程语言是否有用.好不好,不仅体现在语言本身,更在语言的生态系统:用的人多不多.社区是否活跃互帮互助.语言的相关库和框架质量如何,还有就是已有的模块的质量与数量. CPAN(Comprehe ...
- DbUtility-关于DataTable转成List的效率问题
DbUtility中的方法ExecuteDataTableAsync()得到的是一个DataTable,而我们常见的情况下,我们需要的不是DataTable,而是List或IList,所以现在需要考虑 ...
- DbUtility-查询DataTable
直接上源码 using System; using System.Data; using System.Threading.Tasks; using DbUtility; namespace Test ...
- PHP中的常量
常量与变量不一样,一旦被定义就可以全局访问. 例如: <?php define('CONST_NAME','shiyingyan'); define('RESULT_OK',1); define ...
- Android json操作之取得一个对象
1:服务端返回的json数据格式如下: {"id":"1001","name":"zhangsan","sco ...
- RAILS 4 ON RUBY的AJAX实现过程
XXX,最近在笨手笨脚的写一个自动化SVN更新的WEB操作小平台, 这次决定用RAILS 4实现. 但因为网上的教材都是以3或2版本实现的.所以这次搞了不少弯路,现总结如下: 相关的VIEW代码: & ...
- ruby文档
http://www.ruby-doc.org/http://rubyonrails.org/https://www.ruby-lang.org/zh_cn/downloads/http://ruby ...
- POJ_3083——贴左右墙DFS,最短路径BFS
Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and mus ...