Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Logic:one loop on whole array for one elemnt of triplet and other two element will be found by two-pointer.
- */
- class Solution:
- def threeSum(self, nums: List[int]) -> List[List[int]]:
- nums.sort()
- ans=[]
- for i in range(len(nums)):
- if i>0 and nums[i]==nums[i-1]:
- continue
- j=i+1
- k=len(nums)-1
- while j<k:
- if nums[i]+nums[j]+nums[k]==0:
- ans.append([nums[i],nums[j],nums[k]])
- j+=1
- k-=1
- while j<k and nums[j]==nums[j-1]: #These two while is there if suppose 1M 0 is there in array
- j+=1
- while k>j and nums[k]==nums[k+1]:
- k-=1
- elif nums[i]+nums[j]+nums[k]>0:
- k-=1
- else:
- j+=1
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement