LeetCode15:三数之和 Python

LeetCode15:三数之和 Python_第1张图片

import numpy as np
class Solution:
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        
#       法一:时间复杂度高
#         if(len(nums)<3):
#             return []
#         nums.sort()
#         l=[]
#         l1=len(nums)-2
#         l2=len(nums)-1
#         for i in range(0,l1):
#             for j in range(i+1,l2):
#                 for k in range(j+1,len(nums)):
#                     if(nums[k]+nums[i]+nums[j]==0):
#                         l.append((nums[i],nums[j],nums[k]))

#         reList = list(set([tuple(t) for t in l]))
#         reList = [list(v) for v in reList]
#         #print(reList)
#         return reList

    #法二:指针
        if(len(nums)<3):
            return []
        nums.sort()
        #print(nums)
        l=[] 
        for i in range(0,len(nums)-2):
            if(nums[i]*3>0):
                break
            if(i!=0 and nums[i]==nums[i-1]):
                continue
            Pleft=i+1
            Pright=len(nums)-1
            targetsum=0-nums[i]
            while(Plefttargetsum):
                    Pright-=1            
                elif(summ

你可能感兴趣的:(LeetCode,LeetCode,python)