15. Sum of three Java

Keywords: Java

Problem description

Given a n array of N integers nums, judge whether there are three elements a, b, c in nums, so that a + b + c = 0? Find all triples that meet the conditions and are not repeated.

Note: the answer cannot contain duplicate triples.

For example, given the array nums = [-1, 0, 1, 2, -1, -4],

The required set of triples is:
[
[-1, 0, 1],
[-1, -1, 2]
]

problem analysis

It can be solved by sorting the array first, then using double pointer and the idea of sum of two numbers of sorting array. Time O(n*n)

The Java code is as follows:

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length < 3) {
            return result;
        }
        // The array is sorted first, and then the sum of the two numbers is made by using double pointers. O(n*n)
        Arrays.sort(nums);
        // Note: only sorting arrays can use the following solution
        for (int i = 0; i < nums.length; i++) {
            // De duplicate the first level elements (the purpose of de duplication is to prevent duplicate triple results.)
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int j = i + 1;
            int k = nums.length - 1;
            while (j < k) {
                int sum = nums[i] + nums[j] + nums[k];
                if (sum == 0) {
                    List<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[j]);
                    list.add(nums[k]);
                    result.add(list);
                    // De reprocessing the second level elements
                    while (j < k && nums[j] == nums[j + 1]) {
                        j++;
                    }
                    while (j < k && nums[k] == nums[k - 1]) {
                        k--;
                    }
                    j++;
                    k--;
                }else if (sum > 0) {
                    k--;
                }else{
                    j++;
                }
            }
        }
        return result;
    }
}

16. Sum of the nearest three

class Solution {
    public int threeSumClosest(int[] nums, int target) {
        if (nums == null || nums.length < 3) {
            return 0;
        }
        // This one doesn't need to be weighed, just find the minimum value.
        Arrays.sort(nums); // Still need to sort first
        int minSum = nums[0] + nums[1] + nums[2];
        for (int k = 0; k < nums.length; k++) {
            int i = k + 1;
            int j = nums.length - 1;
            while (i < j) {
                int curSum = nums[k] + nums[i] + nums[j];
                if (Math.abs(curSum - target) < Math.abs(minSum - target)) {
                    minSum = curSum;
                }
                if (curSum > target) {
                    j--;
                }else if (curSum == target) {
                    return curSum;
                }else{
                    i++;
                }
            }
        }
        return minSum;
    }
}

Posted by rhysmeister on Thu, 02 Jan 2020 20:13:48 -0800