标签:range .so [] 协同 size lse type nbsp turn
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
先排序,再用三个指针协同操作,最后再考虑处理重复数据。
1 class Solution: 2 def threeSum(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: List[List[int]] 6 """ 7 nums.sort() 8 result = [] 9 for i in range(len(nums)-2): 10 if i > 0 and nums[i] == nums[i-1]: 11 continue 12 left, right = i + 1, len(nums) - 1 13 while left < right : 14 sum = nums[i] + nums[left] + nums[right] 15 if sum < 0 : 16 left += 1 17 elif sum > 0 : 18 right -= 1 19 else : 20 result.append([nums[i], nums[left], nums[right]]) 21 while left < right and nums[left] == nums[left + 1]: 22 left += 1 23 while left < right and nums[right] == nums[right - 1]: 24 right -= 1 25 left += 1 26 right -= 1 27 return result
标签:range .so [] 协同 size lse type nbsp turn
原文地址:https://www.cnblogs.com/Luohys/p/10055332.html