Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 36818e7

Browse files
committed
feat: update solutions to lc problem: No.0018,0454
- No.0018.4Sum - No.0454.4Sum II
1 parent bacc49b commit 36818e7

File tree

5 files changed

+81
-80
lines changed

5 files changed

+81
-80
lines changed

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

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49-
"排序 + 双指针"实现。
49+
**方法一:排序 + 双指针**
50+
51+
该题和 [0015.三数之和](../0015.3Sum/README.md) 相似,解法也相似。
52+
53+
时间复杂度为 $O(n^3),ドル空间复杂度为 $O(\log n),ドル其中 $n$ 是数组的长度。
5054

5155
<!-- tabs:start -->
5256

@@ -171,40 +175,35 @@ public:
171175
172176
```go
173177
func fourSum(nums []int, target int) [][]int {
174-
n, res := len(nums), make([][]int, 0)
175-
if n < 4 {
176-
return res
177-
}
178+
ans, n := [][]int{}, len(nums)
178179
sort.Ints(nums)
179-
for i := 0; i < n-3; i++ {
180-
if i > 0 && nums[i] == nums[i-1] {
181-
continue
182-
}
183-
for j := i + 1; j < n-2; j++ {
184-
if j > i+1 && nums[j] == nums[j-1] {
185-
continue
186-
}
187-
k, l := j+1, n-1
188-
for k < l {
189-
if nums[i]+nums[j]+nums[k]+nums[l] == target {
190-
res = append(res, []int{nums[i], nums[j], nums[k], nums[l]})
191-
k++
192-
l--
193-
for k < n && nums[k] == nums[k-1] {
194-
k++
180+
for i := 0; i < n; i++ {
181+
for j := i + 1; j < n; j++ {
182+
for l, r := j+1, n-1; l < r; {
183+
if nums[i]+nums[j]+nums[l]+nums[r] == target {
184+
ans = append(ans, []int{nums[i], nums[j], nums[l], nums[r]})
185+
l, r = l+1, r-1
186+
for l < r && nums[l] == nums[l-1] {
187+
l++
195188
}
196-
for l > j && nums[l] == nums[l+1] {
197-
l--
189+
for l < r && nums[r] == nums[r+1] {
190+
r--
198191
}
199-
} else if nums[i]+nums[j]+nums[k]+nums[l] < target {
200-
k++
192+
} else if nums[i]+nums[j]+nums[l]+nums[r] < target {
193+
l++
201194
} else {
202-
l--
195+
r--
203196
}
204197
}
198+
for j+1 < n && nums[j+1] == nums[j] {
199+
j++
200+
}
201+
}
202+
for i+1 < n && nums[i+1] == nums[i] {
203+
i++
205204
}
206205
}
207-
return res
206+
return ans
208207
}
209208
```
210209

‎solution/0000-0099/0018.4Sum/README_EN.md‎

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040

4141
## Solutions
4242

43+
**Approach 1: Two Pointers**
44+
45+
Time complexity $O(n^3),ドル Space complexity $O(\log n)$.
46+
4347
<!-- tabs:start -->
4448

4549
### **Python3**
@@ -159,40 +163,35 @@ public:
159163
160164
```go
161165
func fourSum(nums []int, target int) [][]int {
162-
n, res := len(nums), make([][]int, 0)
163-
if n < 4 {
164-
return res
165-
}
166+
ans, n := [][]int{}, len(nums)
166167
sort.Ints(nums)
167-
for i := 0; i < n-3; i++ {
168-
if i > 0 && nums[i] == nums[i-1] {
169-
continue
170-
}
171-
for j := i + 1; j < n-2; j++ {
172-
if j > i+1 && nums[j] == nums[j-1] {
173-
continue
174-
}
175-
k, l := j+1, n-1
176-
for k < l {
177-
if nums[i]+nums[j]+nums[k]+nums[l] == target {
178-
res = append(res, []int{nums[i], nums[j], nums[k], nums[l]})
179-
k++
180-
l--
181-
for k < n && nums[k] == nums[k-1] {
182-
k++
168+
for i := 0; i < n; i++ {
169+
for j := i + 1; j < n; j++ {
170+
for l, r := j+1, n-1; l < r; {
171+
if nums[i]+nums[j]+nums[l]+nums[r] == target {
172+
ans = append(ans, []int{nums[i], nums[j], nums[l], nums[r]})
173+
l, r = l+1, r-1
174+
for l < r && nums[l] == nums[l-1] {
175+
l++
183176
}
184-
for l > j && nums[l] == nums[l+1] {
185-
l--
177+
for l < r && nums[r] == nums[r+1] {
178+
r--
186179
}
187-
} else if nums[i]+nums[j]+nums[k]+nums[l] < target {
188-
k++
180+
} else if nums[i]+nums[j]+nums[l]+nums[r] < target {
181+
l++
189182
} else {
190-
l--
183+
r--
191184
}
192185
}
186+
for j+1 < n && nums[j+1] == nums[j] {
187+
j++
188+
}
189+
}
190+
for i+1 < n && nums[i+1] == nums[i] {
191+
i++
193192
}
194193
}
195-
return res
194+
return ans
196195
}
197196
```
198197

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,31 @@
11
func fourSum(nums []int, target int) [][]int {
2-
n, res := len(nums), make([][]int, 0)
3-
if n < 4 {
4-
return res
5-
}
2+
ans, n := [][]int{}, len(nums)
63
sort.Ints(nums)
7-
for i := 0; i < n-3; i++ {
8-
if i > 0 && nums[i] == nums[i-1] {
9-
continue
10-
}
11-
for j := i + 1; j < n-2; j++ {
12-
if j > i+1 && nums[j] == nums[j-1] {
13-
continue
14-
}
15-
k, l := j+1, n-1
16-
for k < l {
17-
if nums[i]+nums[j]+nums[k]+nums[l] == target {
18-
res = append(res, []int{nums[i], nums[j], nums[k], nums[l]})
19-
k++
20-
l--
21-
for k < n && nums[k] == nums[k-1] {
22-
k++
4+
for i := 0; i < n; i++ {
5+
for j := i + 1; j < n; j++ {
6+
for l, r := j+1, n-1; l < r; {
7+
if nums[i]+nums[j]+nums[l]+nums[r] == target {
8+
ans = append(ans, []int{nums[i], nums[j], nums[l], nums[r]})
9+
l, r = l+1, r-1
10+
for l < r && nums[l] == nums[l-1] {
11+
l++
2312
}
24-
for l >j && nums[l] == nums[l+1] {
25-
l--
13+
for l <r && nums[r] == nums[r+1] {
14+
r--
2615
}
27-
} else if nums[i]+nums[j]+nums[k]+nums[l] < target {
28-
k++
16+
} else if nums[i]+nums[j]+nums[l]+nums[r] < target {
17+
l++
2918
} else {
30-
l--
19+
r--
3120
}
3221
}
22+
for j+1 < n && nums[j+1] == nums[j] {
23+
j++
24+
}
25+
}
26+
for i+1 < n && nums[i+1] == nums[i] {
27+
i++
3328
}
3429
}
35-
return res
36-
}
30+
return ans
31+
}

‎solution/0400-0499/0454.4Sum II/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:分组 + 哈希表**
54+
55+
时间复杂度为 $O(n^2),ドル空间复杂度为 $O(n^2),ドル其中 $n$ 是数组的长度。
56+
5357
<!-- tabs:start -->
5458

5559
### **Python3**

‎solution/0400-0499/0454.4Sum II/README_EN.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ The two tuples are:
4444

4545
## Solutions
4646

47+
**Approach 1: HashMap**
48+
49+
Time complexity $O(n^2),ドル Space complexity $O(n^2)$.
50+
4751
<!-- tabs:start -->
4852

4953
### **Python3**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /