Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def threeSum(self, nums):
- """
- :type nums: List[int]
- :rtype: List[List[int]]
- """
- ans = []
- a = sorted(nums)
- n = len(nums)
- for i in range(n):
- target = a[i]
- if(target>0):
- break
- if(i>0 and target==a[i-1]):
- continue
- left = i+1;
- right = n-1
- while(left<right):
- tmp = a[left]+a[right]
- if target + tmp == 0:
- ans.append([target, a[left], a[right]])
- while(left<right and a[left]==a[left+1]):
- left+=1
- while(left<right and a[right]==a[right-1]):
- right-=1
- right-=1
- left+=1
- elif target + tmp < 0:
- left+=1
- elif target + tmp > 0:
- right-=1
- return ans
Add Comment
Please, Sign In to add comment