Question

Formatted question description: https://leetcode.ca/all/15.html

15 -	3 Sum

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?
Find all unique triplets in the array which gives the sum of zero.

Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.

    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)

@tag-array

Algorithm

Sort the original array, and then start traversing the sorted array.

Note that the traversal is not to the last stop, but to the third from the bottom. Here we can do a pruning optimization first, that is, break when traversing to a positive number, Because the array is now ordered, if the first number to be fixed is a positive number, then the following numbers if they are all positive numbers, the sum will never be zero.

Then add the process of skipping if repeated. The processing method is to start with the second number. If it is equal to the previous number, skip it because you don’t want to fix the same number twice.

For the traversed number, subtract the fix number from 0 to get a target, and then only need to find the sum of the two numbers is equal to the target. Use two pointers to point to the first and last two numbers of the array starting after the fix number.

  • If the sum of the two numbers happens to be the target, then the two numbers and the fix number are stored in the result together. Then is to skip the step of repeating numbers. Both pointers need to detect repeated numbers.
  • If the sum of the two numbers is less than the target, move the pointer i on the left one bit to the right to increase the number pointed to.
  • In the same way, if the sum of the two numbers is greater than the target, move the pointer j on the right to the left to reduce the number pointed to

Code

Java

public class Three_Sum {
	public static void main(String[] args) {
	    List<List<Integer>> list = new ArrayList<>();
	    int[] a = new int[]{3,2,1};

	    Arrays.sort(a);
	    System.out.println(a[0]);
	}

    // time: O(N)
    // space: O(1)
	public class Solution {

	    List<List<Integer>> list = new ArrayList<>();

	    public List<List<Integer>> threeSum(int[] nums) {

	        if(nums.length < 3) {
	            return list;
	        }

	        Arrays.sort(nums);

	        // hold one pointer, other two pointer moving
	        for(int ancher = 0; ancher < nums.length; ancher++) {

	            if(ancher > 0 && nums[ancher] == nums[ancher - 1]) {
	                continue;
                }

	            int i = ancher + 1;
	            int j = nums.length -1;

	            while(i < j) {

	                if(nums[ancher] + nums[i] + nums[j] == 0) {

	                    // @note: Arrays.asList()
	                    list.add(Arrays.asList(nums[ancher], nums[i], nums[j]));

	                    i++;
	                    j--;

	                    // @note: optimization. above i,j is updated already, compare with previous position
	                    while(i < j && nums[i] == nums[i - 1]) {
	                        i++;
	                    }
	                    while(j > i && nums[j] == nums[j + 1]) {
	                        j--;
	                    }

	                } else if(nums[ancher] + nums[i] + nums[j] < 0) {
	                    i++;

	                    // @note: same here, possibly updated already, note i-1 or i+1
	                    while(i < j && nums[i] == nums[i - 1]) {
	                        i++;
	                    }
	                } else {
	                    j--;

	                    // @note: same here, possibly updated already, note i-1 or i+1
	                    while(j > i && j + 1 < nums.length && nums[j] == nums[j + 1]) {
	                        j--;
	                    }
	                }
                }

	        }

	        return list;
	    }
	}

}

All Problems

All Solutions