52
52
``` python
53
53
class Solution :
54
54
def fourSum (self , nums : List[int ], target : int ) -> List[List[int ]]:
55
- res = []
56
- if nums is None or len (nums) < 4 :
57
- return res
58
- n = len (nums)
55
+ n, res = len (nums), []
56
+ if n < 4 :
57
+ return []
59
58
nums.sort()
60
59
for i in range (n - 3 ):
61
60
if i > 0 and nums[i] == nums[i - 1 ]:
62
61
continue
63
62
for j in range (i + 1 , n - 2 ):
64
63
if j > i + 1 and nums[j] == nums[j - 1 ]:
65
64
continue
66
- p, q = j + 1 , n - 1
67
- while p < q:
68
- if p > j + 1 and nums[p] == nums[p - 1 ]:
69
- p += 1
70
- continue
71
- if q < n - 1 and nums[q] == nums[q + 1 ]:
72
- q -= 1
73
- continue
74
- t = nums[i] + nums[j] + nums[p] + nums[q]
75
- if t == target:
76
- res.append([nums[i], nums[j], nums[p], nums[q]])
77
- p += 1
78
- q -= 1
79
- elif t < target:
80
- p += 1
65
+ k, l = j + 1 , n - 1
66
+ while k < l:
67
+ if nums[i] + nums[j] + nums[k] + nums[l] == target:
68
+ res.append([nums[i], nums[j], nums[k], nums[l]])
69
+ k += 1
70
+ l -= 1
71
+ while k < n and nums[k] == nums[k - 1 ]:
72
+ k += 1
73
+ while l > j and nums[l] == nums[l + 1 ]:
74
+ l -= 1
75
+ elif nums[i] + nums[j] + nums[k] + nums[l] < target:
76
+ k += 1
81
77
else :
82
- q -= 1
78
+ l -= 1
83
79
return res
84
80
```
85
81
@@ -90,8 +86,8 @@ class Solution:
90
86
``` java
91
87
class Solution {
92
88
public List<List<Integer > > fourSum (int [] nums , int target ) {
93
- int n;
94
- if (nums == null || (n = (nums . length)) < 4 ) {
89
+ int n = nums . length ;
90
+ if (n < 4 ) {
95
91
return Collections . emptyList();
96
92
}
97
93
Arrays . sort(nums);
@@ -104,31 +100,106 @@ class Solution {
104
100
if (j > i + 1 && nums[j] == nums[j - 1 ]) {
105
101
continue ;
106
102
}
107
- int p = j + 1 , q = n - 1 ;
108
- while (p < q) {
109
- if (p > j + 1 && nums[p] == nums[p - 1 ]) {
110
- ++ p;
111
- continue ;
112
- }
113
- if (q < n - 1 && nums[q] == nums[q + 1 ]) {
114
- -- q;
115
- continue ;
103
+ int k = j + 1 , l = n - 1 ;
104
+ while (k < l) {
105
+ if (nums[i] + nums[j] + nums[k] + nums[l] == target) {
106
+ res. add(Arrays . asList(nums[i], nums[j], nums[k], nums[l]));
107
+ ++ k;
108
+ -- l;
109
+ while (k < n && nums[k] == nums[k - 1 ]) {
110
+ ++ k;
111
+ }
112
+ while (l > j && nums[l] == nums[l + 1 ]) {
113
+ -- l;
114
+ }
115
+ } else if (nums[i] + nums[j] + nums[k] + nums[l] < target) {
116
+ ++ k;
117
+ } else {
118
+ -- l;
116
119
}
117
- int t = nums[i] + nums[j] + nums[p] + nums[q];
118
- if (t == target) {
119
- res. add(Arrays . asList(nums[i], nums[j], nums[p], nums[q]));
120
- ++ p;
121
- -- q;
122
- } else if (t < target) {
123
- ++ p;
120
+ }
121
+ }
122
+ }
123
+ return res;
124
+ }
125
+ }
126
+ ```
127
+
128
+ ### ** C++**
129
+
130
+ ``` cpp
131
+ class Solution {
132
+ public:
133
+ vector<vector<int >> fourSum(vector<int >& nums, int target) {
134
+ int n = nums.size();
135
+ if (n < 4) {
136
+ return {};
137
+ }
138
+ sort(nums.begin(), nums.end());
139
+ vector<vector<int >> res;
140
+ for (int i = 0; i < n - 3; ++i) {
141
+ if (i > 0 && nums[ i] == nums[ i - 1] ) continue;
142
+ for (int j = i + 1; j < n - 2; ++j) {
143
+ if (j > i + 1 && nums[ j] == nums[ j - 1] ) continue;
144
+ int k = j + 1, l = n - 1;
145
+ while (k < l) {
146
+ if (nums[ i] + nums[ j] == target - nums[ k] - nums[ l] ) {
147
+ res.push_back({nums[ i] , nums[ j] , nums[ k] , nums[ l] });
148
+ ++k;
149
+ --l;
150
+ while (k < n && nums[ k] == nums[ k - 1] ) ++k;
151
+ while (l > j && nums[ l] == nums[ l + 1] ) --l;
152
+ } else if (nums[ i] + nums[ j] < target - nums[ k] - nums[ l] ) {
153
+ ++k;
124
154
} else {
125
- -- q ;
155
+ --l ;
126
156
}
127
157
}
128
158
}
129
159
}
130
160
return res;
131
161
}
162
+ };
163
+ ```
164
+
165
+ ### **Go**
166
+
167
+ ```go
168
+ func fourSum(nums []int, target int) [][]int {
169
+ n, res := len(nums), make([][]int, 0)
170
+ if n < 4 {
171
+ return res
172
+ }
173
+ sort.Ints(nums)
174
+ for i := 0; i < n-3; i++ {
175
+ if i > 0 && nums[i] == nums[i-1] {
176
+ continue
177
+ }
178
+ for j := i + 1; j < n-2; j++ {
179
+ if j > i+1 && nums[j] == nums[j-1] {
180
+ continue
181
+ }
182
+ k, l := j+1, n-1
183
+ for k < l {
184
+ if nums[i]+nums[j]+nums[k]+nums[l] == target {
185
+ res = append(res, []int{nums[i], nums[j], nums[k], nums[l]})
186
+ k++
187
+ l--
188
+ for k < n && nums[k] == nums[k-1] {
189
+ k++
190
+ }
191
+ for l > j && nums[l] == nums[l+1] {
192
+ l--
193
+ }
194
+ } else if nums[i]+nums[j]+nums[k]+nums[l] < target {
195
+ k++
196
+ } else {
197
+ l--
198
+ }
199
+ }
200
+ }
201
+ }
202
+ return res
132
203
}
133
204
```
134
205
@@ -139,32 +210,28 @@ class Solution {
139
210
* @param {number} target
140
211
* @return {number[][]}
141
212
*/
142
- var fourSum = function (nums , target ) {
143
- let len = nums .length ;
213
+ var fourSum = function (nums , target ) {
214
+ const n = nums .length ;
215
+ if (n < 4 ) return [];
144
216
let res = [];
145
- if (len < 4 ) return [];
146
217
nums .sort ((a , b ) => a - b);
147
- for (i = 0 ; i < len - 3 ; i++ ) {
148
- if (i > 0 && nums[i] === nums[i - 1 ]) continue ;
149
- if (nums[i] + nums[len - 1 ] + nums[len - 2 ] + nums[len - 3 ] < target) continue ;
150
- if (nums[i] + nums[i + 1 ] + nums[i + 2 ] + nums[i + 3 ] > target) break ;
151
- for (j = i + 1 ; j < len - 2 ; j++ ) {
152
- if (j > i + 1 && nums[j] === nums[j - 1 ]) continue ;
153
- let left = j + 1 , right = len - 1 ;
154
- while (left < right) {
155
- if (nums[i] + nums[j] + nums[left] + nums[right] === target) {
156
- res .push ([nums[i], nums[j], nums[left], nums[right]]);
157
- while (nums[left] === nums[left + 1 ]) left++ ;
158
- left++ ;
159
- while (nums[right] === nums[right - 1 ]) right-- ;
160
- right-- ;
161
- continue ;
162
- } else if (nums[i] + nums[j] + nums[left] + nums[right] > target) {
163
- right-- ;
164
- continue ;
218
+ for (let i = 0 ; i < n - 3 ; ++ i) {
219
+ if (i > 0 && nums[i] == nums[i - 1 ]) continue ;
220
+ for (let j = i + 1 ; j < n - 2 ; ++ j) {
221
+ if (j > i + 1 && nums[j] == nums[j - 1 ]) continue ;
222
+ let k = j + 1 ;
223
+ let l = n - 1 ;
224
+ while (k < l) {
225
+ if (nums[i] + nums[j] + nums[k] + nums[l] == target) {
226
+ res .push ([nums[i], nums[j], nums[k], nums[l]]);
227
+ ++ k;
228
+ -- l;
229
+ while (k < n && nums[k] == nums[k - 1 ]) ++ k;
230
+ while (l > j && nums[l] == nums[l + 1 ]) -- l;
231
+ } else if (nums[i] + nums[j] + nums[k] + nums[l] < target) {
232
+ ++ k;
165
233
} else {
166
- left++ ;
167
- continue ;
234
+ -- l;
168
235
}
169
236
}
170
237
}
0 commit comments