Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
72 lines (60 loc) · 2.5 KB

File metadata and controls

72 lines (60 loc) · 2.5 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

Difficulty: Medium

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]

Solution

Language: Java

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        if (nums.length < 4) {
            return Collections.emptyList();
        }
        Arrays.sort(nums);
        Set<List<Integer>> result = new HashSet<>(); // 一开始写的是list,不过这个去重真的是很麻烦,还是使用set来去重了
        for (int i = 0; i < nums.length - 1; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            for (int j = i + 1; j < nums.length; j++) {
                int targetSum = target - (nums[i] + nums[j]);
                if (targetSum < nums[j] && nums[j] > 0) { // 因为已经排序了,所以后面的任意两个数和必然大于 targetSum
                    break;
                }
                for (int x = j + 1, y = nums.length - 1; x < y; ) {
                    if (nums[x] + nums[y] > targetSum) {
                        y--;
                    } else if (nums[x] + nums[y] < targetSum) {
                        x++;
                    } else {
                        result.add(Arrays.asList(nums[i], nums[j], nums[x], nums[y]));
                        x++;
                        y--;
​
                        while (x < y && x >= 1 && nums[x] == nums[x - 1]) {
                            x++;
                        }
                        while (x < y && y + 1 < nums.length && nums[y] == nums[y + 1]) {
                            y--;
                        }
                    }
                }
​
            }
        }
        return new ArrayList<>(result);
    }
}

Morty Proxy This is a proxified and sanitized view of the page, visit original site.