【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】
【015-3 Sum(三个数的和)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
题目大意
给定一个n个元素的数组。是否存在a,b,c三个元素。使用得a+b+c=0,找出全部符合这个条件的三元组。
解题思路
能够在 2sum问题 的基础上来解决3sum问题,如果3sum问题的目标是target。
每次从数组中选出一个数k。从剩下的数中求目标等于target-k的2sum问题。这里须要注意的是有个小的trick:当我们从数组中选出第i数时,我们仅仅须要求数值中从第i+1个到最后一个范围内字数组的2sum问题。
我们以选第一个和第二个举例。如果数组为A[],总共同拥有n个元素A1。A2….An。非常显然,当选出A1时,我们在子数组[A2~An]中求目标位target-A1的2sum问题,我们要证明的是当选出A2时,我们仅仅须要在子数组[A3~An]中计算目标位target-A2的2sum问题,而不是在子数组[A1,A3~An]中。
证明例如以下:如果在子数组[A1,A3~An]目标位target-A2的2sum问题中,存在A1 + m = target-A2(m为A3~An中的某个数),即A2 + m = target-A1。这刚好是“对于子数组[A3~An],目标位target-A1的2sum问题”的一个解。
即我们相当于对满足3sum的三个数A1+A2+m = target反复计算了。
因此为了避免反复计算,在子数组[A1,A3~An]中,能够把A1去掉,再来计算目标是target-A2的2sum问题。
对于本题要求的求最接近解,仅仅须要保存当前解以及当前解和目标的距离,如果新的解更接近,则更新解。算法复杂度为O(n^2);
代码实现
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class Solution {
/**
* 015-3 Sum(三个数的和)
*
* @param nums 输入的数组
* @return 运行结果
*/
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new LinkedList<>();
if (nums != null && nums.length > 2) {
// 先对数组进行排序
Arrays.sort(nums);
// i表示如果取第i个数作为结果
for (int i = 0; i < nums.length - 2; ) {
// 第二个数可能的起始位置
int j = i + 1;
// 第三个数可能是结束位置
int k = nums.length - 1;
while (j < k) {
// 如果找到满足条件的解
if (nums[j] + nums[k] == -nums[i]) {
// 将结果加入到结果含集中
List<Integer> list = new ArrayList<>(3);
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[k]);
result.add(list);
// 移动到下一个位置。找下一组解
k--;
j++;
// 从左向右找第一个与之前处理的数不同的数的下标
while (j < k && nums[j] == nums[j - 1]) {
j++;
}
// 从右向左找第一个与之前处理的数不同的数的下标
while (j < k && nums[k] == nums[k + 1]) {
k--;
}
}
// 和大于0
else if (nums[j] + nums[k] > -nums[i]) {
k--;
// 从右向左找第一个与之前处理的数不同的数的下标
while (j < k && nums[k] == nums[k + 1]) {
k--;
}
}
// 和小于0
else {
j++;
// 从左向右找第一个与之前处理的数不同的数的下标
while (j < k && nums[j] == nums[j - 1]) {
j++;
}
}
}
// 指向下一个要处理的数
i++;
// 从左向右找第一个与之前处理的数不同的数的下标
while (i < nums.length - 2 && nums[i] == nums[i - 1]) {
i++;
}
}
}
return result;
}
}
评測结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/46980229】
【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】的更多相关文章
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
[053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
[062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...
- 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
[059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...
- 【LeetCode-面试算法经典-Java实现】【136-Single Number(仅仅出现一次的数字)】
[136-Single Number(仅仅出现一次的数字)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array of integers, ev ...
- 【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】
[075-Sort Colors (颜色排序)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array with n objects colore ...
- 【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】
[101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, check whether ...
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
[109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 ...
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】
[030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...
随机推荐
- python bisect模块二分法查找
#!/usr/bin/env python # encoding: utf-8 import bisect import sys #将一个元素插入到一个有序列表的合适位置 #使用这个模块的函数前先确保 ...
- mysq 中 information_schema 库
information_schema这个库,这个在mysql安装时就有了,提供了访问数据库元数据的方式.那什么是元数据库呢?元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等.有些时 ...
- 使用navicat连接linux服务器数据库方法
主机名用localhost 要使用SSH连接
- Educational Codeforces Round 33 (Rated for Div. 2) C. Rumor【并查集+贪心/维护集合最小值】
C. Rumor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- Linked List Random Node -- LeetCode
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- 【bzoj4152】【The Captain】最短路(堆优化)(浅尝ACM-B)
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62834538 Description 给定平面上 ...
- 集合框架(中):Map
Map接口: Map提供了一种映射关系,其中的元素就是以键值对(key-value)的形式存储的,能够实现根据key快速查找value Map中的键值对以Entry类型的对象实例形式存在 键(key值 ...
- linux下安装php报错configure: error: Cannot find MySQL header files under /usr/include/mysql.
linux下安装php报错configure: error: Cannot find MySQL header files under /usr/include/mysql. 2013-03-04 1 ...
- PHP 让__get方法重新执行
<?php class A { public function __get($p) { echo "getting $p\r\n"; if(isset($this->$ ...
- ASIHTTPRequest框架使用总结系列之阿堂教程5(上传数据)
在上篇文章中,阿堂和网友们分享了如何用ASIHTTPRequest框架下载数据的实例,本篇阿堂将数据介绍如何用ASIHTTPRequest框架上传数据的应用实例. 数据上传是通过ASIHT ...