Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: 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]
]

解法:

  首先对原数组进行排序,然后开始遍历排序后的数组,这里注意不是遍历到最后一个停止,而是到倒数第三个就可以了,中间如果遇到跟前一个数相同的数字就直接跳过。对于遍历到的数,如果大于0则跳到下一个数,因为三个大于0的数相加不可能等于0;否则用0减去这个数得到一个sum,我们只需要再之后找到两个数之和等于sum即可,这样一来问题又转化为了求two sum,这时候我们一次扫描,找到了等于sum的两数后,加上当前遍历到的数字,按顺序存入结果中即可,然后还要注意跳过重复数字。时间复杂度为 O(n2)。代码如下:

public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>(); // 注意不能是new List<>(); List是接口 if (nums == null || nums.length < 3) {
return res;
} Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
if (nums[i] > 0) {
break;
}
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
} int sum = -nums[i];
int left = i + 1, right = nums.length - 1; while (left < right) {
if (nums[left] + nums[right] == sum) {
ArrayList<Integer> triplet = new ArrayList<>();
triplet.add(nums[i]);
triplet.add(nums[left]);
triplet.add(nums[right]);
res.add(triplet); while (left < right && nums[left++] == nums[left]) {}
while (left < right && nums[right--] == nums[right]) {} } else if (nums[left] + nums[right] < sum) {
while (left < right && nums[left++] == nums[left]) {} } else {
while (left < right && nums[right--] == nums[right]) {}
}
}
}
return res;
}
}

[LeetCode] 15. 3Sum ☆☆的更多相关文章

  1. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  2. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  3. [LeetCode] 15. 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  4. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

  5. LeetCode 15 3Sum(3个数求和为0的组合)

    题目链接 https://leetcode.com/problems/3sum/?tab=Description   Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...

  6. LeetCode 15. 3Sum(三数之和)

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  7. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  8. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  9. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

  10. Leetcode 15. 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

随机推荐

  1. LogisticRegression Algorithm——机器学习(西瓜书)读书笔记

    import numpy as np from sklearn.datasets import load_breast_cancer import sklearn.linear_model from ...

  2. 六:YARN Node Labels

    参考:http://dongxicheng.org/mapreduce-nextgen/hadoop-yarn-label-based-scheduling/ 为不同的DATANODE打标签,通过标签 ...

  3. Python3 数据类型-元组

    Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 实例1(Python3.0+): tu ...

  4. python学习笔记02:运行python程序

    1.启动cmd命令行,输入python后回车,运行python解释器: 输入python代码后回车: print('Hello World')

  5. PokeCats开发者日志(九)

      现在是PokeCats游戏开发的第十五天的中午,总算过了规范性检查这一关. 但愿能过吧.

  6. Personal summary 个人总结

    一.请回望开学时的第一次作业,你对于软件工程课程的想象 对比开篇博客你对课程目标和期待,"希望通过实践锻炼,增强计算机专业的能力和就业竞争力",对比目前的所学所练所得,在哪些方面达 ...

  7. tweenjs缓动算法使用小实例

    这里的tweenjs不是依托于createjs的tewwnjs,而是一系列缓动算法集合.因为本身是算法,可以用在各个业务场景中,这也正是总结学习它的价值所在.tweenjs代码详情: /* * Twe ...

  8. vi/sed等遵循的搜索正则语法

    转自:http://blog.csdn.net/lanxinju/article/details/5731843 一.查找 查找命令 /pattern<Enter> :向下查找patter ...

  9. [BinaryTree] 二叉树类的实现

    二叉树结点的抽象数据类型: template<class T> class BinaryTreeNode { friend class BinaryTree<T>; priva ...

  10. set(gcf,'DoubleBuffer','on')以及sort

    设置的目的是为了防止在不断循环画动画的时候会产生闪烁的现象,而这样便不会了.在动画的制作比较常用. Matlab排序函数-sort sort函数的调用格式: sort(X) 功能:返回对向量X中的元素 ...