Skip to content

Commit 3755d3f

Browse files
committed
Update solution 0018
1 parent ae5c145 commit 3755d3f

File tree

8 files changed

+212
-39
lines changed

8 files changed

+212
-39
lines changed

README.md

Lines changed: 30 additions & 30 deletions
Large diffs are not rendered by default.

leetcode/0018.4Sum/18. 4Sum.go

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,92 @@ package leetcode
22

33
import "sort"
44

5-
func fourSum(nums []int, target int) [][]int {
5+
// 解法一 双指针
6+
func fourSum(nums []int, target int) (quadruplets [][]int) {
7+
sort.Ints(nums)
8+
n := len(nums)
9+
for i := 0; i < n-3 && nums[i]+nums[i+1]+nums[i+2]+nums[i+3] <= target; i++ {
10+
if i > 0 && nums[i] == nums[i-1] || nums[i]+nums[n-3]+nums[n-2]+nums[n-1] < target {
11+
continue
12+
}
13+
for j := i + 1; j < n-2 && nums[i]+nums[j]+nums[j+1]+nums[j+2] <= target; j++ {
14+
if j > i+1 && nums[j] == nums[j-1] || nums[i]+nums[j]+nums[n-2]+nums[n-1] < target {
15+
continue
16+
}
17+
for left, right := j+1, n-1; left < right; {
18+
if sum := nums[i] + nums[j] + nums[left] + nums[right]; sum == target {
19+
quadruplets = append(quadruplets, []int{nums[i], nums[j], nums[left], nums[right]})
20+
for left++; left < right && nums[left] == nums[left-1]; left++ {
21+
}
22+
for right--; left < right && nums[right] == nums[right+1]; right-- {
23+
}
24+
} else if sum < target {
25+
left++
26+
} else {
27+
right--
28+
}
29+
}
30+
}
31+
}
32+
return
33+
}
34+
35+
// 解法二 kSum
36+
func fourSum1(nums []int, target int) [][]int {
37+
res, cur := make([][]int, 0), make([]int, 0)
38+
sort.Ints(nums)
39+
kSum(nums, 0, len(nums)-1, target, 4, cur, &res)
40+
return res
41+
}
42+
43+
func kSum(nums []int, left, right int, target int, k int, cur []int, res *[][]int) {
44+
if right-left+1 < k || k < 2 || target < nums[left]*k || target > nums[right]*k {
45+
return
46+
}
47+
if k == 2 {
48+
// 2 sum
49+
twoSum(nums, left, right, target, cur, res)
50+
} else {
51+
for i := left; i < len(nums); i++ {
52+
if i == left || (i > left && nums[i-1] != nums[i]) {
53+
next := make([]int, len(cur))
54+
copy(next, cur)
55+
next = append(next, nums[i])
56+
kSum(nums, i+1, len(nums)-1, target-nums[i], k-1, next, res)
57+
}
58+
}
59+
}
60+
61+
}
62+
63+
func twoSum(nums []int, left, right int, target int, cur []int, res *[][]int) {
64+
for left < right {
65+
sum := nums[left] + nums[right]
66+
if sum == target {
67+
cur = append(cur, nums[left], nums[right])
68+
temp := make([]int, len(cur))
69+
copy(temp, cur)
70+
*res = append(*res, temp)
71+
// reset cur to previous state
72+
cur = cur[:len(cur)-2]
73+
left++
74+
right--
75+
for left < right && nums[left] == nums[left-1] {
76+
left++
77+
}
78+
for left < right && nums[right] == nums[right+1] {
79+
right--
80+
}
81+
} else if sum < target {
82+
left++
83+
} else {
84+
right--
85+
}
86+
}
87+
}
88+
89+
// 解法三
90+
func fourSum2(nums []int, target int) [][]int {
691
res := [][]int{}
792
counter := map[int]int{}
893
for _, value := range nums {

website/content/ChapterFour/0001~0099/0018.4Sum.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,92 @@ package leetcode
4444

4545
import "sort"
4646

47-
func fourSum(nums []int, target int) [][]int {
47+
// 解法一 双指针
48+
func fourSum(nums []int, target int) (quadruplets [][]int) {
49+
sort.Ints(nums)
50+
n := len(nums)
51+
for i := 0; i < n-3 && nums[i]+nums[i+1]+nums[i+2]+nums[i+3] <= target; i++ {
52+
if i > 0 && nums[i] == nums[i-1] || nums[i]+nums[n-3]+nums[n-2]+nums[n-1] < target {
53+
continue
54+
}
55+
for j := i + 1; j < n-2 && nums[i]+nums[j]+nums[j+1]+nums[j+2] <= target; j++ {
56+
if j > i+1 && nums[j] == nums[j-1] || nums[i]+nums[j]+nums[n-2]+nums[n-1] < target {
57+
continue
58+
}
59+
for left, right := j+1, n-1; left < right; {
60+
if sum := nums[i] + nums[j] + nums[left] + nums[right]; sum == target {
61+
quadruplets = append(quadruplets, []int{nums[i], nums[j], nums[left], nums[right]})
62+
for left++; left < right && nums[left] == nums[left-1]; left++ {
63+
}
64+
for right--; left < right && nums[right] == nums[right+1]; right-- {
65+
}
66+
} else if sum < target {
67+
left++
68+
} else {
69+
right--
70+
}
71+
}
72+
}
73+
}
74+
return
75+
}
76+
77+
// 解法二 kSum
78+
func fourSum1(nums []int, target int) [][]int {
79+
res, cur := make([][]int, 0), make([]int, 0)
80+
sort.Ints(nums)
81+
kSum(nums, 0, len(nums)-1, target, 4, cur, &res)
82+
return res
83+
}
84+
85+
func kSum(nums []int, left, right int, target int, k int, cur []int, res *[][]int) {
86+
if right-left+1 < k || k < 2 || target < nums[left]*k || target > nums[right]*k {
87+
return
88+
}
89+
if k == 2 {
90+
// 2 sum
91+
twoSum(nums, left, right, target, cur, res)
92+
} else {
93+
for i := left; i < len(nums); i++ {
94+
if i == left || (i > left && nums[i-1] != nums[i]) {
95+
next := make([]int, len(cur))
96+
copy(next, cur)
97+
next = append(next, nums[i])
98+
kSum(nums, i+1, len(nums)-1, target-nums[i], k-1, next, res)
99+
}
100+
}
101+
}
102+
103+
}
104+
105+
func twoSum(nums []int, left, right int, target int, cur []int, res *[][]int) {
106+
for left < right {
107+
sum := nums[left] + nums[right]
108+
if sum == target {
109+
cur = append(cur, nums[left], nums[right])
110+
temp := make([]int, len(cur))
111+
copy(temp, cur)
112+
*res = append(*res, temp)
113+
// reset cur to previous state
114+
cur = cur[:len(cur)-2]
115+
left++
116+
right--
117+
for left < right && nums[left] == nums[left-1] {
118+
left++
119+
}
120+
for left < right && nums[right] == nums[right+1] {
121+
right--
122+
}
123+
} else if sum < target {
124+
left++
125+
} else {
126+
right--
127+
}
128+
}
129+
}
130+
131+
// 解法三
132+
func fourSum2(nums []int, target int) [][]int {
48133
res := [][]int{}
49134
counter := map[int]int{}
50135
for _, value := range nums {

website/content/ChapterFour/1800~1899/1877.Minimize-Maximum-Pair-Sum-in-Array.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414

1515
```
1616

17+
18+
1719
----------------------------------------------
18-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1800~1899/1846.Maximum-Element-After-Decreasing-and-Rearranging/">⬅️上一页</a></p>
20+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1800~1899/1846.Maximum-Element-After-Decreasing-and-Rearranging/">⬅️上一页</a></p>
21+

website/content/ChapterTwo/Array.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ weight: 1
355355
|1658|Minimum Operations to Reduce X to Zero|[Go]({{< relref "/ChapterFour/1600~1699/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}})|Medium||||33.3%|
356356
|1662|Check If Two String Arrays are Equivalent|[Go]({{< relref "/ChapterFour/1600~1699/1662.Check-If-Two-String-Arrays-are-Equivalent.md" >}})|Easy||||82.0%|
357357
|1664|Ways to Make a Fair Array|[Go]({{< relref "/ChapterFour/1600~1699/1664.Ways-to-Make-a-Fair-Array.md" >}})|Medium||||62.1%|
358-
|1665|Minimum Initial Energy to Finish Tasks|[Go]({{< relref "/ChapterFour/1600~1699/1665.Minimum-Initial-Energy-to-Finish-Tasks.md" >}})|Hard||||55.1%|
358+
|1665|Minimum Initial Energy to Finish Tasks|[Go]({{< relref "/ChapterFour/1600~1699/1665.Minimum-Initial-Energy-to-Finish-Tasks.md" >}})|Hard||||55.0%|
359359
|1670|Design Front Middle Back Queue|[Go]({{< relref "/ChapterFour/1600~1699/1670.Design-Front-Middle-Back-Queue.md" >}})|Medium||||54.7%|
360360
|1672|Richest Customer Wealth|[Go]({{< relref "/ChapterFour/1600~1699/1672.Richest-Customer-Wealth.md" >}})|Easy||||88.2%|
361361
|1673|Find the Most Competitive Subsequence|[Go]({{< relref "/ChapterFour/1600~1699/1673.Find-the-Most-Competitive-Subsequence.md" >}})|Medium||||46.4%|
@@ -366,7 +366,7 @@ weight: 1
366366
|1684|Count the Number of Consistent Strings|[Go]({{< relref "/ChapterFour/1600~1699/1684.Count-the-Number-of-Consistent-Strings.md" >}})|Easy||||81.8%|
367367
|1685|Sum of Absolute Differences in a Sorted Array|[Go]({{< relref "/ChapterFour/1600~1699/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array.md" >}})|Medium||||63.8%|
368368
|1690|Stone Game VII|[Go]({{< relref "/ChapterFour/1600~1699/1690.Stone-Game-VII.md" >}})|Medium||||58.7%|
369-
|1691|Maximum Height by Stacking Cuboids|[Go]({{< relref "/ChapterFour/1600~1699/1691.Maximum-Height-by-Stacking-Cuboids.md" >}})|Hard||||51.1%|
369+
|1691|Maximum Height by Stacking Cuboids|[Go]({{< relref "/ChapterFour/1600~1699/1691.Maximum-Height-by-Stacking-Cuboids.md" >}})|Hard||||51.2%|
370370
|1695|Maximum Erasure Value|[Go]({{< relref "/ChapterFour/1600~1699/1695.Maximum-Erasure-Value.md" >}})|Medium||||52.3%|
371371
|1696|Jump Game VI|[Go]({{< relref "/ChapterFour/1600~1699/1696.Jump-Game-VI.md" >}})|Medium||||41.9%|
372372
|1700|Number of Students Unable to Eat Lunch|[Go]({{< relref "/ChapterFour/1700~1799/1700.Number-of-Students-Unable-to-Eat-Lunch.md" >}})|Easy||||67.7%|

website/content/ChapterTwo/Binary_Search.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func peakIndexInMountainArray(A []int) int {
170170
|0611|Valid Triangle Number|[Go]({{< relref "/ChapterFour/0600~0699/0611.Valid-Triangle-Number.md" >}})|Medium||||49.0%|
171171
|0633|Sum of Square Numbers|[Go]({{< relref "/ChapterFour/0600~0699/0633.Sum-of-Square-Numbers.md" >}})|Medium||||33.0%|
172172
|0658|Find K Closest Elements|[Go]({{< relref "/ChapterFour/0600~0699/0658.Find-K-Closest-Elements.md" >}})|Medium||||43.3%|
173-
|0668|Kth Smallest Number in Multiplication Table|[Go]({{< relref "/ChapterFour/0600~0699/0668.Kth-Smallest-Number-in-Multiplication-Table.md" >}})|Hard||||48.5%|
173+
|0668|Kth Smallest Number in Multiplication Table|[Go]({{< relref "/ChapterFour/0600~0699/0668.Kth-Smallest-Number-in-Multiplication-Table.md" >}})|Hard||||48.4%|
174174
|0704|Binary Search|[Go]({{< relref "/ChapterFour/0700~0799/0704.Binary-Search.md" >}})|Easy||||54.9%|
175175
|0710|Random Pick with Blacklist|[Go]({{< relref "/ChapterFour/0700~0799/0710.Random-Pick-with-Blacklist.md" >}})|Hard| O(n)| O(n) ||33.3%|
176176
|0718|Maximum Length of Repeated Subarray|[Go]({{< relref "/ChapterFour/0700~0799/0718.Maximum-Length-of-Repeated-Subarray.md" >}})|Medium||||51.1%|

website/content/ChapterTwo/Dynamic_Programming.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ weight: 7
101101
|1664|Ways to Make a Fair Array|[Go]({{< relref "/ChapterFour/1600~1699/1664.Ways-to-Make-a-Fair-Array.md" >}})|Medium||||62.1%|
102102
|1681|Minimum Incompatibility|[Go]({{< relref "/ChapterFour/1600~1699/1681.Minimum-Incompatibility.md" >}})|Hard||||36.1%|
103103
|1690|Stone Game VII|[Go]({{< relref "/ChapterFour/1600~1699/1690.Stone-Game-VII.md" >}})|Medium||||58.7%|
104-
|1691|Maximum Height by Stacking Cuboids|[Go]({{< relref "/ChapterFour/1600~1699/1691.Maximum-Height-by-Stacking-Cuboids.md" >}})|Hard||||51.1%|
104+
|1691|Maximum Height by Stacking Cuboids|[Go]({{< relref "/ChapterFour/1600~1699/1691.Maximum-Height-by-Stacking-Cuboids.md" >}})|Hard||||51.2%|
105105
|1696|Jump Game VI|[Go]({{< relref "/ChapterFour/1600~1699/1696.Jump-Game-VI.md" >}})|Medium||||41.9%|
106106
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|
107107

website/content/ChapterTwo/Sorting.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ weight: 14
106106
|1647|Minimum Deletions to Make Character Frequencies Unique|[Go]({{< relref "/ChapterFour/1600~1699/1647.Minimum-Deletions-to-Make-Character-Frequencies-Unique.md" >}})|Medium||||55.8%|
107107
|1648|Sell Diminishing-Valued Colored Balls|[Go]({{< relref "/ChapterFour/1600~1699/1648.Sell-Diminishing-Valued-Colored-Balls.md" >}})|Medium||||31.4%|
108108
|1657|Determine if Two Strings Are Close|[Go]({{< relref "/ChapterFour/1600~1699/1657.Determine-if-Two-Strings-Are-Close.md" >}})|Medium||||54.8%|
109-
|1665|Minimum Initial Energy to Finish Tasks|[Go]({{< relref "/ChapterFour/1600~1699/1665.Minimum-Initial-Energy-to-Finish-Tasks.md" >}})|Hard||||55.1%|
109+
|1665|Minimum Initial Energy to Finish Tasks|[Go]({{< relref "/ChapterFour/1600~1699/1665.Minimum-Initial-Energy-to-Finish-Tasks.md" >}})|Hard||||55.0%|
110110
|1679|Max Number of K-Sum Pairs|[Go]({{< relref "/ChapterFour/1600~1699/1679.Max-Number-of-K-Sum-Pairs.md" >}})|Medium||||53.5%|
111-
|1691|Maximum Height by Stacking Cuboids|[Go]({{< relref "/ChapterFour/1600~1699/1691.Maximum-Height-by-Stacking-Cuboids.md" >}})|Hard||||51.1%|
111+
|1691|Maximum Height by Stacking Cuboids|[Go]({{< relref "/ChapterFour/1600~1699/1691.Maximum-Height-by-Stacking-Cuboids.md" >}})|Hard||||51.2%|
112112
|1710|Maximum Units on a Truck|[Go]({{< relref "/ChapterFour/1700~1799/1710.Maximum-Units-on-a-Truck.md" >}})|Easy||||72.5%|
113113
|1818|Minimum Absolute Sum Difference|[Go]({{< relref "/ChapterFour/1800~1899/1818.Minimum-Absolute-Sum-Difference.md" >}})|Medium||||28.4%|
114114
|1846|Maximum Element After Decreasing and Rearranging|[Go]({{< relref "/ChapterFour/1800~1899/1846.Maximum-Element-After-Decreasing-and-Rearranging.md" >}})|Medium||||55.4%|

0 commit comments

Comments
 (0)