Skip to content

Commit

Permalink
feat: update solutions to lc problem: No.0018,0454
Browse files Browse the repository at this point in the history
- No.0018.4Sum
- No.0454.4Sum II
  • Loading branch information
poltao committed Nov 16, 2022
1 parent bacc49b commit 36818e7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 80 deletions.
53 changes: 26 additions & 27 deletions solution/0000-0099/0018.4Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@

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

“排序 + 双指针”实现。
**方法一:排序 + 双指针**

该题和 [0015.三数之和](../0015.3Sum/README.md) 相似,解法也相似。

时间复杂度为 $O(n^3)$,空间复杂度为 $O(\log n)$,其中 $n$ 是数组的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -171,40 +175,35 @@ public:
```go
func fourSum(nums []int, target int) [][]int {
n, res := len(nums), make([][]int, 0)
if n < 4 {
return res
}
ans, n := [][]int{}, len(nums)
sort.Ints(nums)
for i := 0; i < n-3; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
}
for j := i + 1; j < n-2; j++ {
if j > i+1 && nums[j] == nums[j-1] {
continue
}
k, l := j+1, n-1
for k < l {
if nums[i]+nums[j]+nums[k]+nums[l] == target {
res = append(res, []int{nums[i], nums[j], nums[k], nums[l]})
k++
l--
for k < n && nums[k] == nums[k-1] {
k++
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
for l, r := j+1, n-1; l < r; {
if nums[i]+nums[j]+nums[l]+nums[r] == target {
ans = append(ans, []int{nums[i], nums[j], nums[l], nums[r]})
l, r = l+1, r-1
for l < r && nums[l] == nums[l-1] {
l++
}
for l > j && nums[l] == nums[l+1] {
l--
for l < r && nums[r] == nums[r+1] {
r--
}
} else if nums[i]+nums[j]+nums[k]+nums[l] < target {
k++
} else if nums[i]+nums[j]+nums[l]+nums[r] < target {
l++
} else {
l--
r--
}
}
for j+1 < n && nums[j+1] == nums[j] {
j++
}
}
for i+1 < n && nums[i+1] == nums[i] {
i++
}
}
return res
return ans
}
```

Expand Down
51 changes: 25 additions & 26 deletions solution/0000-0099/0018.4Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@

## Solutions

**Approach 1: Two Pointers**

Time complexity $O(n^3)$, Space complexity $O(\log n)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -159,40 +163,35 @@ public:
```go
func fourSum(nums []int, target int) [][]int {
n, res := len(nums), make([][]int, 0)
if n < 4 {
return res
}
ans, n := [][]int{}, len(nums)
sort.Ints(nums)
for i := 0; i < n-3; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
}
for j := i + 1; j < n-2; j++ {
if j > i+1 && nums[j] == nums[j-1] {
continue
}
k, l := j+1, n-1
for k < l {
if nums[i]+nums[j]+nums[k]+nums[l] == target {
res = append(res, []int{nums[i], nums[j], nums[k], nums[l]})
k++
l--
for k < n && nums[k] == nums[k-1] {
k++
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
for l, r := j+1, n-1; l < r; {
if nums[i]+nums[j]+nums[l]+nums[r] == target {
ans = append(ans, []int{nums[i], nums[j], nums[l], nums[r]})
l, r = l+1, r-1
for l < r && nums[l] == nums[l-1] {
l++
}
for l > j && nums[l] == nums[l+1] {
l--
for l < r && nums[r] == nums[r+1] {
r--
}
} else if nums[i]+nums[j]+nums[k]+nums[l] < target {
k++
} else if nums[i]+nums[j]+nums[l]+nums[r] < target {
l++
} else {
l--
r--
}
}
for j+1 < n && nums[j+1] == nums[j] {
j++
}
}
for i+1 < n && nums[i+1] == nums[i] {
i++
}
}
return res
return ans
}
```

Expand Down
49 changes: 22 additions & 27 deletions solution/0000-0099/0018.4Sum/Solution.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
func fourSum(nums []int, target int) [][]int {
n, res := len(nums), make([][]int, 0)
if n < 4 {
return res
}
ans, n := [][]int{}, len(nums)
sort.Ints(nums)
for i := 0; i < n-3; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
}
for j := i + 1; j < n-2; j++ {
if j > i+1 && nums[j] == nums[j-1] {
continue
}
k, l := j+1, n-1
for k < l {
if nums[i]+nums[j]+nums[k]+nums[l] == target {
res = append(res, []int{nums[i], nums[j], nums[k], nums[l]})
k++
l--
for k < n && nums[k] == nums[k-1] {
k++
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
for l, r := j+1, n-1; l < r; {
if nums[i]+nums[j]+nums[l]+nums[r] == target {
ans = append(ans, []int{nums[i], nums[j], nums[l], nums[r]})
l, r = l+1, r-1
for l < r && nums[l] == nums[l-1] {
l++
}
for l > j && nums[l] == nums[l+1] {
l--
for l < r && nums[r] == nums[r+1] {
r--
}
} else if nums[i]+nums[j]+nums[k]+nums[l] < target {
k++
} else if nums[i]+nums[j]+nums[l]+nums[r] < target {
l++
} else {
l--
r--
}
}
for j+1 < n && nums[j+1] == nums[j] {
j++
}
}
for i+1 < n && nums[i+1] == nums[i] {
i++
}
}
return res
}
return ans
}
4 changes: 4 additions & 0 deletions solution/0400-0499/0454.4Sum II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@

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

**方法一:分组 + 哈希表**

时间复杂度为 $O(n^2)$,空间复杂度为 $O(n^2)$,其中 $n$ 是数组的长度。

<!-- tabs:start -->

### **Python3**
Expand Down
4 changes: 4 additions & 0 deletions solution/0400-0499/0454.4Sum II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ The two tuples are:

## Solutions

**Approach 1: HashMap**

Time complexity $O(n^2)$, Space complexity $O(n^2)$.

<!-- tabs:start -->

### **Python3**
Expand Down

0 comments on commit 36818e7

Please sign in to comment.