Likou brush question 2023-04-30-1——Title: Sword Pointer Offer II 007. Three numbers whose sum is 0 in the array

topic:

Given an array of integers  nums , determine whether there is a triple  [nums[i], nums[j], nums[k]] that satisfies  i != j, i != k and  j != k , and satisfies at the same time  nums[i] + nums[j] + nums[k] == 0 . please

You return all  0 triples that sum to and do not repeat.

Note: Duplicate triplets are not allowed in the answer.

Example 1:

Input: nums = [-1,0,1,2,-1,-4]
 Output: [[-1,-1,2],[-1,0,1]]
 Explanation: 
nums[0] + nums [1] + nums[2] = (-1) + 0 + 1 = 0 . 
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 . 
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 . 
The different triples are [-1,0,1] and [-1,-1,2]. 
Note that the order of the output and the order of the triples is not important.

Example 2:

Input: nums = [0,1,1]
 Output: []
 Explanation: The only possible triple sum is not 0.

Example 3:

Input: nums = [0,0,0]
 Output: [[0,0,0]]
 Explanation: The only possible triple sum is 0.

hint:

  • 3 <= nums.length <= 3000
  • -105 <= nums[i] <= 105

 code:

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

        First sort the original array, then enumerate the first number i, and transform the problem into finding two numbers between the i+1th and the last number so that their sum is equal to the negative number i. Use the double pointer method to optimize the search process, the left pointer l is initialized to i+1, and the right pointer r is initialized to the last number of the array. Add the two numbers corresponding to l and r to get the current sum s, and then move the pointer according to the magnitude relationship between s and the negative number i.

        Specifically, if s is less than 0, it means that the number pointed by the left pointer is too small, and the left pointer needs to be moved to the right; if s is greater than 0, it means that the number pointed by the right pointer is too large, and the right pointer needs to be moved to the left; if s If it is equal to 0, it means that a set of qualified triples has been found, added to the result set, and the pointer is moved to skip the repeated numbers.

        The time complexity of the whole code is O(n square), where the time complexity of sorting is O(nlogn), and the time complexity of the search process is O(n), so the time complexity of sorting is the bottleneck, but the sorting Complexity can be optimized by using efficient algorithms such as quicksort.

result:

 

Guess you like

Origin blog.csdn.net/qq_25368751/article/details/130447518