File tree Expand file tree Collapse file tree 2 files changed +50
-32
lines changed
Hard/10. Regular Expression Matching Expand file tree Collapse file tree 2 files changed +50
-32
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public List <List <Integer >> fourSum (int [] nums , int target ) {
3
+ List <List <Integer >> ans = new ArrayList <>();
4
+ Arrays .sort (nums );
5
+
6
+ for (int i = 0 ; i < nums .length - 3 ; i ++) {
7
+ if (i > 0 && nums [i ] == nums [i - 1 ]) {
8
+ continue ;
9
+ }
10
+
11
+ for (int j = i + 1 ; j < nums .length - 2 ; j ++) {
12
+ if (j > i + 1 && nums [j ] == nums [j - 1 ]) {
13
+ continue ;
14
+ }
15
+
16
+ int k = j + 1 ;
17
+ int l = nums .length - 1 ;
18
+
19
+ while (k < l ) {
20
+ long sum = (long ) nums [i ] + nums [j ] + nums [k ] + nums [l ];
21
+
22
+ if (sum == target ) {
23
+ List <Integer > temp = new ArrayList <>();
24
+ temp .add (nums [i ]);
25
+ temp .add (nums [j ]);
26
+ temp .add (nums [k ]);
27
+ temp .add (nums [l ]);
28
+ ans .add (temp );
29
+
30
+ k ++;
31
+ l --;
32
+
33
+ while (k < l && nums [k ] == nums [k - 1 ]) {
34
+ k ++;
35
+ }
36
+
37
+ while (k < l && nums [l ] == nums [l + 1 ]) {
38
+ l --;
39
+ }
40
+ } else if (sum < target ) {
41
+ k ++;
42
+ } else {
43
+ l --;
44
+ }
45
+ }
46
+ }
47
+ }
48
+ return ans ;
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments