-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFourSum.java
More file actions
153 lines (135 loc) · 3.34 KB
/
FourSum.java
File metadata and controls
153 lines (135 loc) · 3.34 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.junit.Test;
import junit.framework.TestCase;
/**
* Leetcode: https://leetcode.com/problems/4sum/
*
* @author shivam.maharshi
*/
public class FourSum extends TestCase {
public List<List<Integer>> fourSum(int[] nums, int target) {
if (nums.length < 4)
return new ArrayList<List<Integer>>();
List<List<Integer>> res = new ArrayList<List<Integer>>();
Set<F> set = new TreeSet<F>();
Arrays.sort(nums);
for (int m = nums.length - 1; m >= 3; m--) {
for (int n = m - 1; n >= 2; n--) {
int num = target -1 * (nums[n] + nums[m]);
int r = n - 1, l = 0;
addCriteriaMatchingFalues(nums, num, r, l, n, m, set);
}
}
populateList(res, set);
return res;
}
public void addCriteriaMatchingFalues(int[] nums, int num, int r, int l, int n, int m, Set<F> set) {
while (r > l) {
int sum = nums[r] + nums[l];
if (sum == num) {
addNum(nums, num, r, l, n, m, set);
r--;
l++;
} else if (sum > num) {
r--;
} else {
l++;
}
}
}
public void addNum(int[] nums, int num, int r, int l, int n, int m, Set<F> set) {
set.add(new F(nums[l], nums[r], nums[n], nums[m]));
}
public void populateList(List<List<Integer>> list, Set<F> set) {
Iterator<F> it = set.iterator();
while (it.hasNext()) {
List<Integer> l = new ArrayList<Integer>();
F v = it.next();
l.add(v.a);
l.add(v.b);
l.add(v.c);
l.add(v.d);
list.add(l);
}
}
@Test
public void test() {
FourSum f = new FourSum();
assertEquals(new ArrayList<List<Integer>>(), f.fourSum(new int[]{0,0,0,0}, 1));
}
}
class F implements Comparable<F> {
public int a;
public int b;
public int c;
public int d;
public F() {
super();
}
public F(int a, int b, int c, int d) {
super();
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
@Override
public int compareTo(F o) {
if (this.a < o.a)
return -1;
else if (this.a == o.a) {
if (this.b < o.b)
return -1;
else if (this.b == o.b) {
if (this.c < o.c)
return -1;
else if (this.c == o.c) {
if (this.d < o.d)
return -1;
else if (this.d == o.d)
return 0;
else
return 1;
} else
return 1;
} else
return 1;
} else
return 1;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + a;
result = prime * result + b;
result = prime * result + c;
result = prime * result + d;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
F other = (F) obj;
if (a != other.a)
return false;
if (b != other.b)
return false;
if (c != other.c)
return false;
if (d != other.d)
return false;
return true;
}
}