[Python-leetcode15-] three double pointer is zero and the number of

Problem Description:

Given an array nums n comprises integers, it determines whether there are three elements a, b, c nums such that a + b + c = 0? All the conditions are not satisfied to find duplicate triples.

Note: The answer can not contain duplicate triples.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

Meet the requirements set of triples as:
[
[-1, 0, 1],
[-1, -1, 2]
]

Code:

class Solution:
     DEF threeSum (Self, the nums):
         # the array length 
        n-= len (the nums)
         IF ( Not the nums or n-<. 3 ):
             return []
         # first sort the array 
        nums.sort ()
         # save the result 
        RES = [ ]
         # from the left through the array 
        for I in Range (n-):
             # when traversing a positive number can be returned to the results 
            IF (the nums [I]> 0):
                 return RES
             # if i> 0 and the adjacent two values equal, then continue
            IF (i> 0 and the nums [i] == the nums [i -. 1 ]):
                 Continue 
            # left pointer points to the next i 
            L = i +. 1 # and the right pointer points to the right end of the array 
            R & lt n-= -. 1 # loop condition # when the core is in bit i, i + 1 from the viewpoint position to the end, by continuously increasing the size of the left pointer value # to find a balanced position to reduce the size and the right pointer value so that the three sum is 0 the while ( L < R & lt):
                 # If these three numbers add up to 0 IF (the nums [I] the nums + [L] + the nums [R & lt] == 0):
                     # added result                     res.append ([nums [i], nums [L], the nums [R & lt]])
                     # at this time the left pointer to the next one is determined, and if it is the same, right left pointer continues the while (L <R & lt
            
            
            
            
            
                

                    and the nums [L] == the nums [L +. 1 ]): 
                        L = L +. 1
                     # Similarly, the left and the right pointer continues 
                    the while (L <R & lt and the nums [R & lt] == the nums [R & lt -. 1 ]): 
                        R & lt R & lt = -. 1
                     # if not identical, to the right a left pointer directly 
                    L = L +. 1 # if not identical, to the left and the right pointer directly to an 
                    R & lt = R & lt -. 1 elif (the nums [I] the nums + [L] + the nums [ R & lt]> 0):
                     # if more than 0, the left one and the right pointer 
                    R & lt = R & lt -. 1 the else :
                     # is less than 0, a left pointer right 
                    L = L + 1
                    
                
                
        return res

result:

Guess you like

Origin www.cnblogs.com/xiximayou/p/12313290.html