File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 22
33import java .util .ArrayList ;
44import java .util .Arrays ;
5+ import java .util .HashSet ;
56import java .util .List ;
7+ import java .util .Set ;
68
79public class _18 {
810
@@ -46,4 +48,28 @@ public List<List<Integer>> fourSum(int[] nums, int target) {
4648 return result ;
4749 }
4850 }
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+ }
4975}
You can’t perform that action at this time.
0 commit comments