File tree 1 file changed +26
-0
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import java .util .ArrayList ;
4
4
import java .util .Arrays ;
5
+ import java .util .HashSet ;
5
6
import java .util .List ;
7
+ import java .util .Set ;
6
8
7
9
public class _18 {
8
10
@@ -46,4 +48,28 @@ public List<List<Integer>> fourSum(int[] nums, int target) {
46
48
return result ;
47
49
}
48
50
}
51
+
52
+ public static class Solution2 {
53
+ public List <List <Integer >> fourSum (int [] nums , int target ) {
54
+ Arrays .sort (nums );
55
+ Set <List <Integer >> set = new HashSet <>();
56
+ for (int i = 0 ; i < nums .length - 3 ; i ++) {
57
+ for (int j = i + 1 ; j < nums .length - 2 ; j ++) {
58
+ for (int k = j + 1 ; k < nums .length - 1 ; k ++) {
59
+ for (int p = k + 1 ; p < nums .length ; p ++) {
60
+ int sum = nums [i ] + nums [j ] + nums [k ] + nums [p ];
61
+ if (sum == target ) {
62
+ set .add (Arrays .asList (nums [i ], nums [j ], nums [k ], nums [p ]));
63
+ }
64
+ }
65
+ }
66
+ }
67
+ }
68
+ List <List <Integer >> result = new ArrayList <>();
69
+ for (List <Integer > each : set ) {
70
+ result .add (each );
71
+ }
72
+ return result ;
73
+ }
74
+ }
49
75
}
You can’t perform that action at this time.
0 commit comments