LeetCode Question 15 - Three and the number of

 

1. Topic

2. Analysis and thinking title

3. Ideas

 

1. Topic

Given an array nums n comprises integers, 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.

For example, given the array nums = [-1, 0, 1, 2, -1, -4],

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

2. Ideas

  This question is the most immediate thought should be the sum of two numbers, two numbers and is quite basic, the use of records notice the way, maintaining a dictionary, to see whether the number of new dictionary key. Three digital sum can also use a similar approach, but the subject of the request is not duplicated , and this more difficult task, and that may be the first of its sort, and then judge for themselves whether they are in the collection, ideas and opinions on the obvious the code is as follows:

def threeSum(self, nums: List[int]) -> List[List[int]]:
        result = []
        for i,j in enumerate(nums):
            temp = nums[:i]+nums[i+1:]
            dic1 = {}
            dic2 = {}
            for count,k in enumerate(temp):
                if k not in dic1:
                    dic1[-j-k] = k
                else:
                    dic2[tuple(sorted([j,k,-j-k]))] = [j,k,-j-k]
        return dic2.values()

3. Improved

    Unfortunately, however, this complexity is too high, but all the case will run overtime, before that I use is to determine whether the list in the list, the more so there is no way through all the case, complexity is too high, after optimization but still in all case 0 failed to use a dictionary.

    After modification, and some boundary conditions, given a passed case, but extremely slow solution, I call it ruthless solution:

DEF threeSum (Self, the nums: List [int]) -> List [List [int]]: 
        DIC2 = {}
         IF (len (SET (the nums)) ==. 1) and (len (the nums)> 2 ): # mainly to remove all zero, all 0 will lead to the last cycle high complexity
             IF 0 in the SET (nums):
                 return [[0,0,0]]
         for i, J in the enumerate (nums): 
            TEMP = the nums [: I] the nums + [I +. 1 :] 
            DIC1 = {}
             for COUNT, K in the enumerate (TEMP):
                 IF K Not  in dic1:
                    dic1[-j-k] = k
                else:
                    dic2[tuple(sorted([j,k,-j-k]))] = [j,k,-j-k]
        return dic2.values()

   Correct solution is given below, using the double pointer

 

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums = sorted(nums)
        res = []
        dic1 = {}
        for i,j in enumerate(nums):
            if j > 0:
                continue
            temp = nums[i+1:]
            left = 0
            right = len(temp)-1
            while(left < right):
                
                if j + temp[left]+temp[right] == 0:
                    dic1[(j,temp[left],temp[right])] = [j,temp[left],temp[right]]
                    # res.append([j,temp[left],temp[right]])
                    right -= 1
                elif j + temp[left]+temp[right] > 0:
                    right -= 1
                else:
                    left += 1
        return dic1.values()

 

Guess you like

Origin www.cnblogs.com/tjpeng/p/11621435.html