class Solution:
# @param {integer[]} nums
# @return {integer[][]}
def threeSum(self, nums):
if nums == None:
return None
if len(nums) == 0:
return []
nums.sort()
intList = []
for i in range(0,len(nums)):
#repeating of first element
if i-1 >= 0 and nums[i] == nums[i-1]:
continue
p = i + 1
q = len(nums) -1
while p>=0 and p<len(nums) and q>=0 and p<q:
if nums[i] + nums[p] + nums[q] == 0:
triple = [nums[i],nums[p],nums[q]]
intList.append(triple)
p+=1
q-=1
#repeating of last two elements
while nums[p-1] == nums[p] and p<len(nums) and p<q:
p+=1
while nums[q] == nums[q+1] and q>=0 and p<q:
q-=1
elif nums[i] + nums[p] + nums[q] > 0:
q-=1
else:
p+=1
return intList
LeetCode #15 3Sum
最新推荐文章于 2019-09-19 15:05:45 发布