# Simple Python solution (easy to understand)

• ``````class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
ans=[]
for l in range(0,len(nums)-2):
i=l+1
r=len(nums)-1
while i<r and i>l:
sum=nums[l]+nums[r]+nums[i]
if sum==0:
a=[nums[l],nums[i],nums[r]]
ans.append(a)
i+=1
elif sum>0:
r-=1
else:
i+=1

ans = set(map(tuple, ans))
return list(ans)``````

• @aditya26 This solution times out on the tests with a very large array of zeros.
I was able to speed up the average runtime by checking if the second and third number are equal then break out of the while loop. This way if there is a large set of identical value we are not going to iterate over that entire set uselessly.

• ``````class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if len(nums) < 3:
return []
nums.sort()
s = set()
for k, v in enumerate(nums):
l, r = k+1, len(nums)-1
while l < r:
sums = v + nums[l] + nums[r]
if sums == 0:
if nums[l] == nums[r]:
break
l += 1
r -= 1
elif sums > 0:
r -= 1
elif sums < 0:
l += 1
return list(s)
``````

I guess this is what @ditya26 means.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.