Niuke.com Brushing Questions-Triples whose sum is 0 in the array

Problem Description

Given an array S with n elements, are there elements a, b, and c in S satisfying a+b+c=0? Find all triples in the array S that meet the conditions.
Note:
The elements in the triples (a, b, c) must be arranged in non-descending order. (That is, a≤b≤c) The
solution set cannot contain repeated triples.

Input description:
Input an array

Output description:
output an array set with the sum of three numbers being 0

Example

Example 1

Enter
[-2,0,1,1,2]

Output
[[-2,0,2],[-2,1,1]]

Solutions

analysis

  1. Using the method of three pointer subscripts, the subscript i can be used as the subscript of the traversal array, left is the left subscript greater than i, and right is the right subscript less than len-1. The movement is determined by judging the size relationship between the three elements and 0 Left subscript or right subscript.

method

  1. A three-pointer subscript method is used.

Code

// 思路1
public class Solution {
    
      
    public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
    
    
        Arrays.sort(num);
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        ArrayList<Integer> t;
        int len = num.length;
        for (int i = 0; i < len - 2; i++) {
    
    //len-3,len-2,len-1
            if (i == 0 || num[i] != num[i - 1]) {
    
    
                int left = i + 1; // 左下标
                int right = len - 1; // 右下标
                while (left < right) {
    
    
                	// 如果三个数字和大于0,右下标左移动
                    while (left < right && (num[i] + num[left] + num[right]) > 0) {
    
    
                        right--;
                    }
                    // 符合条件,添加元素
                    if (left < right && (num[i] + num[left] + num[right]) == 0) {
    
    
                        t = new ArrayList<>();
                        t.add(num[i]);
                        t.add(num[left]);
                        t.add(num[right]);
                        res.add(t);
                        // 如果最小元素相同,左下标右移动
                        while (left < right && num[left] == t.get(1)) {
    
    
                            left++;
                        }
                    } else {
    
    
                        // 如果小于0,则左下标右移动
                        left++;
                    }
                }
            }
        }
        return res;
    }
}

Time complexity analysis: The
first time: n-2 The
second time: n-3
The n-
2th time: 1 and = (n-2) + (n-3) +… + 1 = 1/2 * n^ + 3/2 * n + 1 = O(N^)
The time complexity of java quick sort: O(n * logn)
So the time complexity here is: O(N^)

Space complexity analysis:
extra space t is used here, but t only stores 3 numbers, so the space complexity is: O(1)

If you want to test, you can go directly to the link of Niuke.com to do the test

The triples whose sum is 0 in the array-Niuke.com

Guess you like

Origin blog.csdn.net/qq_35398517/article/details/113731715