LeetCode 18. 4Sum

Description

https://leetcode.com/problems/4sum/description/

Given an array nums of n integers and an integer target, are there elements abc, 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:

Explanation

For 4 sum, we need to find 3 other numbers add with nums[i] to equal to the target.

Then it becomes the 3 sum problem.

For 3 sum, we need to find 2 other numbers add with nums[j] to equal to the target – nums[i].

Then it becomes the 2 sum problem.

We sorted the input array. Then we can use two pointers techniques to solve.

Video Tutorial

Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *