```
import itertools
class Solution(object):
def threeSum(self, nums):
res = []
n = len(nums)
nums.sort()
# O(n^2)
for m in xrange(n): # middle
l = 0 # left
r = n - 1 # right
while 0 <= l < r <= n - 1:
if m == l:
l += 1
continue
elif m == r:
r -= 1
continue
sum = nums[l] + nums[m] + nums[r]
if sum == 0:
res.append(sorted([nums[l], nums[m], nums[r]]))
l += 1
r -= 1
elif sum > 0:
r -= 1
elif sum < 0:
l += 1
# remove all duplicate result, and this is the reason causing slow
res.sort()
return list(u for u, _ in itertools.groupby(res))
```