while loop without removing duplicates on the answer list


  • 0
    Y
     def threeSum(self, nums):
        complete_list = []
        nums2 = sorted(nums)
        tag1 = None
        
        for i in xrange(len(nums2)):
            if nums2[i] == tag1:
                continue
            
            if nums2[i] <= 0:
                j = i + 1
                k = len(nums2) -1
                
                while j < k:                      
                    if nums2[i] + nums2[j]  + nums2[k] == 0:
                        complete_list.append((nums2[i] , nums2[j]  , nums2[k]))
                        while True: 
                            j += 1
                            k -= 1                     
                            if (j >= k) or (nums2[j] > nums2[j-1]) or (nums2[k] < nums2[k+1]):
                                break
                        
                    elif nums2[i] + nums2[j]  + nums2[k] > 0:
                        while True: 
                            k -= 1                     
                            if (j >= k) or (nums2[k] < nums2[k+1]):
                                break
                    
                    else:
                        while True: 
                            j += 1
                            if (j >= k) or (nums2[j] > nums2[j-1]):
                                break             
                    
            else:
                break
            
            tag1 = nums2[i]
        
        return complete_list

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.