Skip to content

Commit f255d95

Browse files
committed
feat: update solutions to lc problem: No.0018. 4Sum
1 parent 72b769a commit f255d95

File tree

8 files changed

+401
-291
lines changed

8 files changed

+401
-291
lines changed

solution/0000-0099/0015.3Sum/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,42 @@ public:
158158
};
159159
```
160160
161+
### **Go**
162+
163+
```go
164+
func threeSum(nums []int) [][]int {
165+
n, res := len(nums), make([][]int, 0)
166+
if n < 3 {
167+
return res
168+
}
169+
sort.Ints(nums)
170+
for i := 0; i < n-2 && nums[i] <= 0; i++ {
171+
if i > 0 && nums[i] == nums[i-1] {
172+
continue
173+
}
174+
j, k := i+1, n-1
175+
for j < k {
176+
if nums[i]+nums[j]+nums[k] == 0 {
177+
res = append(res, []int{nums[i], nums[j], nums[k]})
178+
j++
179+
k--
180+
for j < n && nums[j] == nums[j-1] {
181+
j++
182+
}
183+
for k > i && nums[k] == nums[k+1] {
184+
k--
185+
}
186+
} else if nums[i]+nums[j]+nums[k] < 0 {
187+
j++
188+
} else {
189+
k--
190+
}
191+
}
192+
}
193+
return res
194+
}
195+
```
196+
161197
### **JavaScript**
162198

163199
```js

solution/0000-0099/0018.4Sum/README.md

+129-62
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,30 @@
5252
```python
5353
class Solution:
5454
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 []
5958
nums.sort()
6059
for i in range(n - 3):
6160
if i > 0 and nums[i] == nums[i - 1]:
6261
continue
6362
for j in range(i + 1, n - 2):
6463
if j > i + 1 and nums[j] == nums[j - 1]:
6564
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
8177
else:
82-
q -= 1
78+
l -= 1
8379
return res
8480
```
8581

@@ -90,8 +86,8 @@ class Solution:
9086
```java
9187
class Solution {
9288
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) {
9591
return Collections.emptyList();
9692
}
9793
Arrays.sort(nums);
@@ -104,31 +100,106 @@ class Solution {
104100
if (j > i + 1 && nums[j] == nums[j - 1]) {
105101
continue;
106102
}
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;
116119
}
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;
124154
} else {
125-
--q;
155+
--l;
126156
}
127157
}
128158
}
129159
}
130160
return res;
131161
}
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
132203
}
133204
```
134205

@@ -139,32 +210,28 @@ class Solution {
139210
* @param {number} target
140211
* @return {number[][]}
141212
*/
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 [];
144216
let res = [];
145-
if (len < 4) return [];
146217
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;
165233
} else {
166-
left++;
167-
continue;
234+
--l;
168235
}
169236
}
170237
}

0 commit comments

Comments
 (0)