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)
思考:应该是因为重复的数据仍然需要计算,这耗费了不少时间,如果能够对map中的数据直接处理,可能会好点,仔细一想,跟下面排序的思路有的一拼啊
思路2
:从网上看到的AC代码,前后两个指针,因为如果是有序的数列 a < b < c < d,同时拥有这样的性质:a+c < a+d < b+d;因此找两个数相加,如果比目标大,则后端前移,反之,前端后移~