Advertisement
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]]
- """
- res = []
- nums.sort()
- for i in range(0, len(nums)-2):
- # skip the duplicate
- if i > 0 and nums[i-1] == nums[i]:
- continue
- # Get the target value
- temp = 0 - nums[i]
- # Two pointer from start and end
- l = i + 1
- r = len(nums)-1
- while l < r:
- #在这个while Loop里面要用 if ... elif ... else ...
- if nums[l] + nums[r] == temp:
- # Get the target numbers group
- res.append([nums[i],nums[l],nums[r]])
- # skip the duplicate
- while l < r and nums[l] == nums[l+1]:
- l = l + 1
- while l < r and nums[r] == nums[r-1]:
- r = r - 1
- # Next pair numbers
- l = l + 1
- r = r - 1
- elif nums[l] + nums[r] < temp:
- l = l + 1
- else:
- r = r - 1
- return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement