题面
Given an array nums
of n integers, are there elements a, b, c 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