And the number of Leetcode_18 [Four]

Article Directory:

  • topic
  • A script
  • A script logic
  • Screenplay two
  • Scripts two logic

topic:

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]
]


 

A script: [If] with 1330ms

class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        num1 = len (nums)
        nums.sort()
        res = []
        for i in range(num1-3):
            if nums[i] > target and nums[i] > 0:
                return(res)
            if i > 0 and nums[i] == nums[i-1]:
                continue
            for j in range(i+1,num1-2):
                if j > i+1 and nums[j] == nums[j-1]:
                    continue
                k,l = j+1,num1-1
                while k < l:
                    s = nums[i] + nums[j] + nums[k] + nums[l]
                    if s < target:
                        k = k+1
                        while k < l and nums[k] == nums[k - 1]: k += 1
                    elif s > target:
                        l -= 1
                        while k < l and nums[l] == nums[l + 1]: l -= 1
                    else:
                        res.append([nums[i],nums[j],nums[k],nums[l]])
                        k += 1
                        l -= 1
                        while k < l and nums[k] == nums[k - 1]: k += 1
                        while k < l and nums[l] == nums[l + 1]: l -= 1
        return(res)

 


 

A script logic:

  • The same dual mode traverse pointer
  • By controlling the value of element index and the traversal, the number of combinations of four complete traversal
  • For some cases, traverse skipped, such as the case where a value equal to the previous value

 

Script II: [when using: 80ms] [Reserved]

class Solution(object):
    def fourSum(self, nums, target):
        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:
                return
                
            # two pointers solve sorted 2-sum problem
            if N == 2:
                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
                    elif s < target:
                        L + 1 =
                     presence :
                        r -= 1
            else:
                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

Script two logic:

  • This algorithm real so beautiful
  • Use the idea of ​​recursion, the main processing logic is as follows:
    • First: write pointer processing using a two way two numbers of
    • Second: the use of recursion, to solve three or four, and the number of and the number of recursive or all of the above and the two numbers; this is a key step the following statement:
      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)
  • Pretty algorithm

Guess you like

Origin www.cnblogs.com/mailong/p/12026507.html