-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFourSum.java
More file actions
73 lines (66 loc) · 3.19 KB
/
FourSum.java
File metadata and controls
73 lines (66 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Arrays;
public class FourSum {
public static void main(String[] args) {
int[] num = {0,0,0,0,1};
int target = 1;
System.out.println(threeSum(num,target));
}
public static List<List<Integer>> threeSum(int[] nums, int target) {
List<List<Integer>>ans=new ArrayList<>();
if(nums==null || nums.length==0) return ans;
// real size of array
int n = nums.length;
// sorting this array will enable us to adjust pointers to approximate to the target sum.
Arrays.sort(nums);
// 4-pointers i,j,first,last
// i is the first pointer that increments by one every loop.
for(int i=0;i<n;i++){
// by subtracting the smallest element in the current set we can start approximating the target
long target2=(long)target-(long)nums[i];
// j is the 2nd pointer(i+1) that increments by one every loop.
for(int j=i+1;j<n;j++){
// at this point we'll subtract the first 2 elements and have a remaining value that needs to be comprised of
// 2 distinct index values: "first" and "last."
long remaining=(long)target2-(long)nums[j];
// first is the 3rd pointer(j+1) that increments by one.
int first=j+1;
// last is the 4th pointer and the last element in the array.
int last=n-1;
// while the 3rd pointer is less than the last element pointer
while(first<last){
long twoSum=(long)nums[first]+(long)nums[last];
// if the sum is less than the remnant, we want to increase the left pointer(increase total value)
// if the sum is greater than the remnant, we want to decrease the right pointer(reduce total value)
if(twoSum<remaining) {
first++;
}
else if(twoSum>remaining) {
last--;
}
// this is invoked when twoSum == remaining, in which case we create a new list and and add it to the answer.
else{
List<Integer>res=new ArrayList<>();
res.add(nums[i]);//num 1
res.add(nums[j]);//num 2
res.add(nums[first]);//num 3
res.add(nums[last]);//num 4
ans.add(res);
// Processing the duplicates of number 3
while(first<last && nums[first]==res.get(2)) first++;
// Processing the duplicates of number 4
while(first<last && nums[last]==res.get(3)) last--;
}
}
// Processing the duplicates of number 2
while(j+1<n && nums[j+1]==nums[j]) j++;
}
// Processing the duplicates of number 1
while(i+1<n && nums[i+1]==nums[i]) i++;
}
return ans;
}
}