Array + two points leetcode.15-3Sum

题面

Given an array nums of n integers, are there elements abc in nums 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.

给定数组,找出其中不重复三个和为0的元素集合。

样例

1. Given array nums = [-1, 0, 1, 2, -1, -4],
solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
2. Given array nums = [0, 0, 0],
solution set is:
[
  [0, 0, 0]
]
note: this example may cause something wrong!
(heap overflow? Need you to try it.)

思路

按照我以往的傻瓜思路,暴力来解决的话,time complexity will be O(n3),that's fool.

这里参考了(抄)一个简单易于理解的solution

上一篇:16. 3Sum Closest[M]最接近的三数之和


下一篇:腾讯五十题No.17 螺旋矩阵