@@ -96,6 +96,35 @@ class Solution {
96
96
}
97
97
```
98
98
99
+ ``` csharp
100
+ public class Solution {
101
+ public List <List <int >> FourSum (int [] nums , int target ) {
102
+ int n = nums .Length ;
103
+ Array .Sort (nums );
104
+ HashSet < (int , int , int , int )> res = new HashSet <(int , int , int , int )>();
105
+
106
+ for (int a = 0 ; a < n ; a ++ ) {
107
+ for (int b = a + 1 ; b < n ; b ++ ) {
108
+ for (int c = b + 1 ; c < n ; c ++ ) {
109
+ for (int d = c + 1 ; d < n ; d ++ ) {
110
+ long sum = (long )nums [a ] + nums [b ] + nums [c ] + nums [d ];
111
+ if (sum == target ) {
112
+ res .Add ((nums [a ], nums [b ], nums [c ], nums [d ]));
113
+ }
114
+ }
115
+ }
116
+ }
117
+ }
118
+
119
+ var result = new List <List <int >>();
120
+ foreach (var quad in res ) {
121
+ result .Add (new List <int > { quad .Item1 , quad .Item2 , quad .Item3 , quad .Item4 });
122
+ }
123
+ return result ;
124
+ }
125
+ }
126
+ ```
127
+
99
128
:: tabs-end
100
129
101
130
### Time & Space Complexity
@@ -287,6 +316,58 @@ class Solution {
287
316
}
288
317
```
289
318
319
+ ``` csharp
320
+ public class Solution {
321
+ public List <List <int >> FourSum (int [] nums , int target ) {
322
+ Array .Sort (nums );
323
+ Dictionary < int , int > count = new Dictionary <int , int >();
324
+
325
+ foreach (int num in nums ) {
326
+ if (! count .ContainsKey (num )) {
327
+ count [num ] = 0 ;
328
+ }
329
+ count [num ]++ ;
330
+ }
331
+
332
+ List < List < int >> res = new List <List <int >>();
333
+
334
+ for (int i = 0 ; i < nums .Length ; i ++ ) {
335
+ count [nums [i ]]-- ;
336
+ if (i > 0 && nums [i ] == nums [i - 1 ]) continue ;
337
+
338
+ for (int j = i + 1 ; j < nums .Length ; j ++ ) {
339
+ count [nums [j ]]-- ;
340
+ if (j > i + 1 && nums [j ] == nums [j - 1 ]) continue ;
341
+
342
+ for (int k = j + 1 ; k < nums .Length ; k ++ ) {
343
+ count [nums [k ]]-- ;
344
+ if (k > j + 1 && nums [k ] == nums [k - 1 ]) continue ;
345
+
346
+ long fourth = (long )target - (long )nums [i ] - (long )nums [j ] - (long )nums [k ];
347
+ if (fourth > int .MaxValue || fourth < int .MinValue ) {
348
+ continue ;
349
+ }
350
+
351
+ if (count .ContainsKey ((int )fourth ) && count [(int )fourth ] > 0 ) {
352
+ res .Add (new List <int > { nums [i ], nums [j ], nums [k ], (int )fourth });
353
+ }
354
+ }
355
+
356
+ for (int k = j + 1 ; k < nums .Length ; k ++ ) {
357
+ count [nums [k ]]++ ;
358
+ }
359
+ }
360
+
361
+ for (int j = i + 1 ; j < nums .Length ; j ++ ) {
362
+ count [nums [j ]]++ ;
363
+ }
364
+ }
365
+
366
+ return res ;
367
+ }
368
+ }
369
+ ```
370
+
290
371
:: tabs-end
291
372
292
373
### Time & Space Complexity
@@ -448,6 +529,42 @@ class Solution {
448
529
}
449
530
```
450
531
532
+ ``` csharp
533
+ public class Solution {
534
+ public List <List <int >> FourSum (int [] nums , int target ) {
535
+ Array .Sort (nums );
536
+ List < List < int >> res = new List <List <int >>();
537
+ int n = nums .Length ;
538
+
539
+ for (int i = 0 ; i < n ; i ++ ) {
540
+ if (i > 0 && nums [i ] == nums [i - 1 ]) continue ;
541
+
542
+ for (int j = i + 1 ; j < n ; j ++ ) {
543
+ if (j > i + 1 && nums [j ] == nums [j - 1 ]) continue ;
544
+
545
+ int left = j + 1 , right = n - 1 ;
546
+ while (left < right ) {
547
+ long sum = (long )nums [i ] + nums [j ] + nums [left ] + nums [right ];
548
+ if (sum == target ) {
549
+ res .Add (new List <int > { nums [i ], nums [j ], nums [left ], nums [right ] });
550
+ left ++ ;
551
+ right -- ;
552
+ while (left < right && nums [left ] == nums [left - 1 ]) left ++ ;
553
+ while (left < right && nums [right ] == nums [right + 1 ]) right -- ;
554
+ } else if (sum < target ) {
555
+ left ++ ;
556
+ } else {
557
+ right -- ;
558
+ }
559
+ }
560
+ }
561
+ }
562
+
563
+ return res ;
564
+ }
565
+ }
566
+ ```
567
+
451
568
:: tabs-end
452
569
453
570
### Time & Space Complexity
@@ -637,6 +754,52 @@ class Solution {
637
754
}
638
755
```
639
756
757
+ ``` csharp
758
+ public class Solution {
759
+ private List <List <int >> res ;
760
+ private List <int > quad ;
761
+
762
+ public List <List <int >> FourSum (int [] nums , int target ) {
763
+ Array .Sort (nums );
764
+ res = new List <List <int >>();
765
+ quad = new List <int >();
766
+ KSum (nums , 4 , 0 , target );
767
+ return res ;
768
+ }
769
+
770
+ private void KSum (int [] nums , int k , int start , long target ) {
771
+ if (k == 2 ) {
772
+ int l = start , r = nums .Length - 1 ;
773
+ while (l < r ) {
774
+ long sum = (long )nums [l ] + nums [r ];
775
+ if (sum < target ) {
776
+ l ++ ;
777
+ } else if (sum > target ) {
778
+ r -- ;
779
+ } else {
780
+ List < int > newQuad = new List <int >(quad );
781
+ newQuad .Add (nums [l ]);
782
+ newQuad .Add (nums [r ]);
783
+ res .Add (newQuad );
784
+ l ++ ;
785
+ r -- ;
786
+ while (l < r && nums [l ] == nums [l - 1 ]) l ++ ;
787
+ while (l < r && nums [r ] == nums [r + 1 ]) r -- ;
788
+ }
789
+ }
790
+ return ;
791
+ }
792
+
793
+ for (int i = start ; i < nums .Length - k + 1 ; i ++ ) {
794
+ if (i > start && nums [i ] == nums [i - 1 ]) continue ;
795
+ quad .Add (nums [i ]);
796
+ KSum (nums , k - 1 , i + 1 , target - nums [i ]);
797
+ quad .RemoveAt (quad .Count - 1 );
798
+ }
799
+ }
800
+ }
801
+ ```
802
+
640
803
:: tabs-end
641
804
642
805
### Time & Space Complexity
0 commit comments