参考了:
https://www.cnblogs.com/zuoyuan/p/3699159.html
但是里面的子while循环,来判断相邻两个数相等其实可以不要,因为在第一个while 里面判断了。这样可以提高运算速度。
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
sums = 0
list_all = []
if len(nums) < 3:
return []
for i in range(len(nums) -2):
if i == 0 or nums[i] > nums[i-1]:
point_left = i + 1 ; point_right = len(nums) - 1
while point_right > point_left:
sums = nums[i] + nums[point_left] + nums[point_right]
if sums == 0:
list_all.append([nums[i], nums[point_left], nums[point_right]])
point_left += 1; point_right -= 1
while point_left < point_right and nums[point_left] == nums[point_left-1] :
point_left += 1
while point_left < point_right and nums[point_right] == nums[point_right+1] :
point_right -= 1
elif sums < 0:
#while point_right > point_left:
# point_left += 1
# if nums[point_left] > nums[point_left-1]: break
point_left += 1
else:
point_right -= 1
#while point_right > point_left:
# point_right -= 1
# if nums[point_right] < nums[point_right+1]: break
return list_all
~
~