1-3

18. sum Noriyuki four number

Given a n array of integers and a target nums target, if there are four elements a, b, c, and d nums determined such that a + b + c + d is equal to the value of the target? Identify all satisfy the conditions of the quad and do not repeat.

note:

The answer can not contain duplicate quad.

Example:

Given array nums = [1, 0, -1, 0, -2, 2], and the target = 0.

Meet the requirements set for the four-tuple:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]]

Python most votes solution:

class Solution(object):
    def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        def findNsum(l, r, target, N, result, results):
            if r-l+1 < N or N < 2 or target < nums[l]*N or target > nums[r]*N:  # early termination
                return
            if N == 2: # two pointers solve sorted 2-sum problem
                while l < r:
                    s = nums[l] + nums[r]
                    if s == target:
                        results.append(result + [nums[l], nums[r]])
                        l += 1
                        while l < r and nums[l] == nums[l-1]:
                            l += 1
                        # r -= 1
                        # while l < r and nums[r] == nums[r+1]:
                        #     r -= 1
                    elif s < target:
                        l += 1
                        # while l < r and nums[l] == nums[l-1]:
                        #     l += 1
                    else:
                        r -= 1
                        # while l < r and nums[r] == nums[r+1]:
                        #     r -= 1
            else: # recursively reduce N
                for i in range(l, r+1):
                    if i == l or (i > l and nums[i-1] != nums[i]):
                        findNsum(i+1, r, target-nums[i], N-1, result+[nums[i]], results)

        nums.sort()
        results = []
        findNsum(0, len(nums)-1, target, 4, [], results)
        return results

analysis:

  • The code provides a general solution N-sum (N> 2, hereinafter the same) problem: by an element of the truncated nums, may be the initial N-sum is transformed into (N-1) -sum Problem so continue to cut and continuously transformed, may be the original problem boils down to the 2-sum problem.

    The program uses a recursive way to implement this idea.

  • Core Solution 2-sum problem is to use the left and right hands, by constantly moving the pointer and constantly test, and eventually traverse the entire array nums identify all eligible element values.

  • The while loop will bring additional operational overhead: the program annotated part can theoretically make the algorithm faster, but in the actual testing and found that the for loop will significantly slow down the operation speed of the algorithm, without these algorithms for loop instead faster.

  • The above implementation must be based on an ordered array, it is necessary to first sort nums in the implementation of the algorithm.

Guess you like

Origin www.cnblogs.com/tbgatgb/p/11100023.html
1-3