Skip to content

Commit fcc3d10

Browse files
halfrostdezhiy
authored andcommitted
update solution 0031 & 0048
1 parent 20ae5d8 commit fcc3d10

File tree

14 files changed

+197
-77
lines changed

14 files changed

+197
-77
lines changed

README.md

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

leetcode/0031.Next-Permutation/31. Next Permutation.go

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,58 @@
1-
// https://leetcode.com/problems/next-permutation/discuss/1554932/Go-Submission-with-Explanation
2-
// Time O(N) , Space: O(1)
3-
41
package leetcode
52

6-
// [2,(3),6,5,4,1] -> 2,(4),6,5,(3),1 -> 2,4, 1,3,5,6
3+
// 解法一
74
func nextPermutation(nums []int) {
5+
i, j := 0, 0
6+
for i = len(nums) - 2; i >= 0; i-- {
7+
if nums[i] < nums[i+1] {
8+
break
9+
}
10+
}
11+
if i >= 0 {
12+
for j = len(nums) - 1; j > i; j-- {
13+
if nums[j] > nums[i] {
14+
break
15+
}
16+
}
17+
swap(&nums, i, j)
18+
}
19+
reverse(&nums, i+1, len(nums)-1)
20+
}
21+
22+
func reverse(nums *[]int, i, j int) {
23+
for i < j {
24+
swap(nums, i, j)
25+
i++
26+
j--
27+
}
28+
}
29+
30+
func swap(nums *[]int, i, j int) {
31+
(*nums)[i], (*nums)[j] = (*nums)[j], (*nums)[i]
32+
}
33+
34+
// 解法二
35+
// [2,(3),6,5,4,1] -> 2,(4),6,5,(3),1 -> 2,4, 1,3,5,6
36+
func nextPermutation1(nums []int) {
837
var n = len(nums)
938
var pIdx = checkPermutationPossibility(nums)
1039
if pIdx == -1 {
11-
reverse(nums, 0, n-1)
40+
reverse(&nums, 0, n-1)
1241
return
1342
}
1443

1544
var rp = len(nums) - 1
1645
// start from right most to leftward,find the first number which is larger than PIVOT
1746
for rp > 0 {
1847
if nums[rp] > nums[pIdx] {
19-
swap(nums, pIdx, rp)
48+
swap(&nums, pIdx, rp)
2049
break
2150
} else {
2251
rp--
2352
}
2453
}
2554
// Finally, Reverse all elements which are right from pivot
26-
reverse(nums, pIdx+1, n-1)
27-
}
28-
29-
func swap(nums []int, i, j int) {
30-
nums[i], nums[j] = nums[j], nums[i]
31-
}
32-
33-
func reverse(nums []int, s int, e int) {
34-
for s < e {
35-
swap(nums, s, e)
36-
s++
37-
e--
38-
}
55+
reverse(&nums, pIdx+1, n-1)
3956
}
4057

4158
// checkPermutationPossibility returns 1st occurrence Index where

leetcode/0048.Rotate-Image/48. Rotate Image.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
// Time: O(N) Space: O(1)
2-
31
package leetcode
42

3+
// 解法一
54
func rotate(matrix [][]int) {
5+
length := len(matrix)
6+
// rotate by diagonal 对角线变换
7+
for i := 0; i < length; i++ {
8+
for j := i + 1; j < length; j++ {
9+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
10+
}
11+
}
12+
// rotate by vertical centerline 竖直轴对称翻转
13+
for i := 0; i < length; i++ {
14+
for j := 0; j < length/2; j++ {
15+
matrix[i][j], matrix[i][length-j-1] = matrix[i][length-j-1], matrix[i][j]
16+
}
17+
}
18+
}
19+
20+
// 解法二
21+
func rotate1(matrix [][]int) {
622
n := len(matrix)
723
if n == 1 {
824
return

website/content/ChapterFour/0001~0099/0031.Next-Permutation.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Output: [1]
5757
```go
5858
package leetcode
5959

60+
// 解法一
6061
func nextPermutation(nums []int) {
6162
i, j := 0, 0
6263
for i = len(nums) - 2; i >= 0; i-- {
@@ -86,6 +87,47 @@ func reverse(nums *[]int, i, j int) {
8687
func swap(nums *[]int, i, j int) {
8788
(*nums)[i], (*nums)[j] = (*nums)[j], (*nums)[i]
8889
}
90+
91+
// 解法二
92+
// [2,(3),6,5,4,1] -> 2,(4),6,5,(3),1 -> 2,4, 1,3,5,6
93+
func nextPermutation1(nums []int) {
94+
var n = len(nums)
95+
var pIdx = checkPermutationPossibility(nums)
96+
if pIdx == -1 {
97+
reverse(&nums, 0, n-1)
98+
return
99+
}
100+
101+
var rp = len(nums) - 1
102+
// start from right most to leftward,find the first number which is larger than PIVOT
103+
for rp > 0 {
104+
if nums[rp] > nums[pIdx] {
105+
swap(&nums, pIdx, rp)
106+
break
107+
} else {
108+
rp--
109+
}
110+
}
111+
// Finally, Reverse all elements which are right from pivot
112+
reverse(&nums, pIdx+1, n-1)
113+
}
114+
115+
// checkPermutationPossibility returns 1st occurrence Index where
116+
// value is in decreasing order(from right to left)
117+
// returns -1 if not found(it's already in its last permutation)
118+
func checkPermutationPossibility(nums []int) (idx int) {
119+
// search right to left for 1st number(from right) that is not in increasing order
120+
var rp = len(nums) - 1
121+
for rp > 0 {
122+
if nums[rp-1] < nums[rp] {
123+
idx = rp - 1
124+
return idx
125+
}
126+
rp--
127+
}
128+
return -1
129+
}
130+
89131
```
90132

91133

website/content/ChapterFour/0001~0099/0048.Rotate-Image.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ You have to rotate the image **[in-place](https://en.wikipedia.org/wiki/In-plac
9999
```go
100100
package leetcode
101101
102+
// 解法一
102103
func rotate(matrix [][]int) {
103104
length := len(matrix)
104105
// rotate by diagonal 对角线变换
@@ -114,6 +115,50 @@ func rotate(matrix [][]int) {
114115
}
115116
}
116117
}
118+
119+
// 解法二
120+
func rotate1(matrix [][]int) {
121+
n := len(matrix)
122+
if n == 1 {
123+
return
124+
}
125+
/* rotate clock-wise = 1. transpose matrix => 2. reverse(matrix[i])
126+
127+
1 2 3 4 1 5 9 13 13 9 5 1
128+
5 6 7 8 => 2 6 10 14 => 14 10 6 2
129+
9 10 11 12 3 7 11 15 15 11 7 3
130+
13 14 15 16 4 8 12 16 16 12 8 4
131+
132+
*/
133+
134+
for i := 0; i < n; i++ {
135+
// transpose, i=rows, j=columns
136+
// j = i+1, coz diagonal elements didn't change in a square matrix
137+
for j := i + 1; j < n; j++ {
138+
swap(matrix, i, j)
139+
}
140+
// reverse each row of the image
141+
matrix[i] = reverse(matrix[i])
142+
}
143+
}
144+
145+
// swap changes original slice's i,j position
146+
func swap(nums [][]int, i, j int) {
147+
nums[i][j], nums[j][i] = nums[j][i], nums[i][j]
148+
}
149+
150+
// reverses a row of image, matrix[i]
151+
func reverse(nums []int) []int {
152+
var lp, rp = 0, len(nums) - 1
153+
154+
for lp < rp {
155+
nums[lp], nums[rp] = nums[rp], nums[lp]
156+
lp++
157+
rp--
158+
}
159+
return nums
160+
}
161+
117162
```
118163

119164

website/content/ChapterTwo/Array.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ weight: 1
105105
|0315|Count of Smaller Numbers After Self|[Go]({{< relref "/ChapterFour/0300~0399/0315.Count-of-Smaller-Numbers-After-Self.md" >}})|Hard||||42.0%|
106106
|0318|Maximum Product of Word Lengths|[Go]({{< relref "/ChapterFour/0300~0399/0318.Maximum-Product-of-Word-Lengths.md" >}})|Medium||||56.0%|
107107
|0322|Coin Change|[Go]({{< relref "/ChapterFour/0300~0399/0322.Coin-Change.md" >}})|Medium||||39.0%|
108-
|0324|Wiggle Sort II|[Go]({{< relref "/ChapterFour/0300~0399/0324.Wiggle-Sort-II.md" >}})|Medium||||31.6%|
108+
|0324|Wiggle Sort II|[Go]({{< relref "/ChapterFour/0300~0399/0324.Wiggle-Sort-II.md" >}})|Medium||||31.5%|
109109
|0327|Count of Range Sum|[Go]({{< relref "/ChapterFour/0300~0399/0327.Count-of-Range-Sum.md" >}})|Hard||||36.0%|
110110
|0347|Top K Frequent Elements|[Go]({{< relref "/ChapterFour/0300~0399/0347.Top-K-Frequent-Elements.md" >}})|Medium||||63.7%|
111111
|0349|Intersection of Two Arrays|[Go]({{< relref "/ChapterFour/0300~0399/0349.Intersection-of-Two-Arrays.md" >}})|Easy||||67.5%|
@@ -199,7 +199,7 @@ weight: 1
199199
|0733|Flood Fill|[Go]({{< relref "/ChapterFour/0700~0799/0733.Flood-Fill.md" >}})|Easy||||56.9%|
200200
|0735|Asteroid Collision|[Go]({{< relref "/ChapterFour/0700~0799/0735.Asteroid-Collision.md" >}})|Medium||||44.1%|
201201
|0739|Daily Temperatures|[Go]({{< relref "/ChapterFour/0700~0799/0739.Daily-Temperatures.md" >}})|Medium||||66.1%|
202-
|0744|Find Smallest Letter Greater Than Target|[Go]({{< relref "/ChapterFour/0700~0799/0744.Find-Smallest-Letter-Greater-Than-Target.md" >}})|Easy||||45.9%|
202+
|0744|Find Smallest Letter Greater Than Target|[Go]({{< relref "/ChapterFour/0700~0799/0744.Find-Smallest-Letter-Greater-Than-Target.md" >}})|Easy||||45.8%|
203203
|0746|Min Cost Climbing Stairs|[Go]({{< relref "/ChapterFour/0700~0799/0746.Min-Cost-Climbing-Stairs.md" >}})|Easy| O(n)| O(1)||55.9%|
204204
|0752|Open the Lock|[Go]({{< relref "/ChapterFour/0700~0799/0752.Open-the-Lock.md" >}})|Medium||||55.0%|
205205
|0766|Toeplitz Matrix|[Go]({{< relref "/ChapterFour/0700~0799/0766.Toeplitz-Matrix.md" >}})|Easy| O(n)| O(1)||66.8%|

website/content/ChapterTwo/Binary_Search.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func peakIndexInMountainArray(A []int) int {
176176
|0710|Random Pick with Blacklist|[Go]({{< relref "/ChapterFour/0700~0799/0710.Random-Pick-with-Blacklist.md" >}})|Hard| O(n)| O(n) ||33.1%|
177177
|0718|Maximum Length of Repeated Subarray|[Go]({{< relref "/ChapterFour/0700~0799/0718.Maximum-Length-of-Repeated-Subarray.md" >}})|Medium||||51.2%|
178178
|0719|Find K-th Smallest Pair Distance|[Go]({{< relref "/ChapterFour/0700~0799/0719.Find-K-th-Smallest-Pair-Distance.md" >}})|Hard||||33.8%|
179-
|0744|Find Smallest Letter Greater Than Target|[Go]({{< relref "/ChapterFour/0700~0799/0744.Find-Smallest-Letter-Greater-Than-Target.md" >}})|Easy||||45.9%|
179+
|0744|Find Smallest Letter Greater Than Target|[Go]({{< relref "/ChapterFour/0700~0799/0744.Find-Smallest-Letter-Greater-Than-Target.md" >}})|Easy||||45.8%|
180180
|0778|Swim in Rising Water|[Go]({{< relref "/ChapterFour/0700~0799/0778.Swim-in-Rising-Water.md" >}})|Hard||||58.1%|
181181
|0786|K-th Smallest Prime Fraction|[Go]({{< relref "/ChapterFour/0700~0799/0786.K-th-Smallest-Prime-Fraction.md" >}})|Hard||||47.0%|
182182
|0793|Preimage Size of Factorial Zeroes Function|[Go]({{< relref "/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function.md" >}})|Hard||||41.0%|

website/content/ChapterTwo/Breadth_First_Search.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ weight: 10
7878
|1202|Smallest String With Swaps|[Go]({{< relref "/ChapterFour/1200~1299/1202.Smallest-String-With-Swaps.md" >}})|Medium||||51.0%|
7979
|1203|Sort Items by Groups Respecting Dependencies|[Go]({{< relref "/ChapterFour/1200~1299/1203.Sort-Items-by-Groups-Respecting-Dependencies.md" >}})|Hard||||49.0%|
8080
|1254|Number of Closed Islands|[Go]({{< relref "/ChapterFour/1200~1299/1254.Number-of-Closed-Islands.md" >}})|Medium||||62.7%|
81-
|1302|Deepest Leaves Sum|[Go]({{< relref "/ChapterFour/1300~1399/1302.Deepest-Leaves-Sum.md" >}})|Medium||||85.6%|
81+
|1302|Deepest Leaves Sum|[Go]({{< relref "/ChapterFour/1300~1399/1302.Deepest-Leaves-Sum.md" >}})|Medium||||85.5%|
8282
|1306|Jump Game III|[Go]({{< relref "/ChapterFour/1300~1399/1306.Jump-Game-III.md" >}})|Medium||||61.6%|
8383
|1319|Number of Operations to Make Network Connected|[Go]({{< relref "/ChapterFour/1300~1399/1319.Number-of-Operations-to-Make-Network-Connected.md" >}})|Medium||||56.3%|
8484
|1631|Path With Minimum Effort|[Go]({{< relref "/ChapterFour/1600~1699/1631.Path-With-Minimum-Effort.md" >}})|Medium||||51.0%|

website/content/ChapterTwo/Depth_First_Search.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ weight: 9
4242
|0337|House Robber III|[Go]({{< relref "/ChapterFour/0300~0399/0337.House-Robber-III.md" >}})|Medium||||52.5%|
4343
|0341|Flatten Nested List Iterator|[Go]({{< relref "/ChapterFour/0300~0399/0341.Flatten-Nested-List-Iterator.md" >}})|Medium||||57.5%|
4444
|0385|Mini Parser|[Go]({{< relref "/ChapterFour/0300~0399/0385.Mini-Parser.md" >}})|Medium||||35.3%|
45-
|0386|Lexicographical Numbers|[Go]({{< relref "/ChapterFour/0300~0399/0386.Lexicographical-Numbers.md" >}})|Medium||||56.9%|
45+
|0386|Lexicographical Numbers|[Go]({{< relref "/ChapterFour/0300~0399/0386.Lexicographical-Numbers.md" >}})|Medium||||57.0%|
4646
|0399|Evaluate Division|[Go]({{< relref "/ChapterFour/0300~0399/0399.Evaluate-Division.md" >}})|Medium||||55.9%|
4747
|0404|Sum of Left Leaves|[Go]({{< relref "/ChapterFour/0400~0499/0404.Sum-of-Left-Leaves.md" >}})|Easy||||54.2%|
4848
|0417|Pacific Atlantic Water Flow|[Go]({{< relref "/ChapterFour/0400~0499/0417.Pacific-Atlantic-Water-Flow.md" >}})|Medium||||46.6%|
@@ -105,7 +105,7 @@ weight: 9
105105
|1202|Smallest String With Swaps|[Go]({{< relref "/ChapterFour/1200~1299/1202.Smallest-String-With-Swaps.md" >}})|Medium||||51.0%|
106106
|1203|Sort Items by Groups Respecting Dependencies|[Go]({{< relref "/ChapterFour/1200~1299/1203.Sort-Items-by-Groups-Respecting-Dependencies.md" >}})|Hard||||49.0%|
107107
|1254|Number of Closed Islands|[Go]({{< relref "/ChapterFour/1200~1299/1254.Number-of-Closed-Islands.md" >}})|Medium||||62.7%|
108-
|1302|Deepest Leaves Sum|[Go]({{< relref "/ChapterFour/1300~1399/1302.Deepest-Leaves-Sum.md" >}})|Medium||||85.6%|
108+
|1302|Deepest Leaves Sum|[Go]({{< relref "/ChapterFour/1300~1399/1302.Deepest-Leaves-Sum.md" >}})|Medium||||85.5%|
109109
|1305|All Elements in Two Binary Search Trees|[Go]({{< relref "/ChapterFour/1300~1399/1305.All-Elements-in-Two-Binary-Search-Trees.md" >}})|Medium||||78.5%|
110110
|1306|Jump Game III|[Go]({{< relref "/ChapterFour/1300~1399/1306.Jump-Game-III.md" >}})|Medium||||61.6%|
111111
|1319|Number of Operations to Make Network Connected|[Go]({{< relref "/ChapterFour/1300~1399/1319.Number-of-Operations-to-Make-Network-Connected.md" >}})|Medium||||56.3%|

website/content/ChapterTwo/Hash_Table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ weight: 13
129129
|1128|Number of Equivalent Domino Pairs|[Go]({{< relref "/ChapterFour/1100~1199/1128.Number-of-Equivalent-Domino-Pairs.md" >}})|Easy||||45.8%|
130130
|1160|Find Words That Can Be Formed by Characters|[Go]({{< relref "/ChapterFour/1100~1199/1160.Find-Words-That-Can-Be-Formed-by-Characters.md" >}})|Easy||||67.8%|
131131
|1170|Compare Strings by Frequency of the Smallest Character|[Go]({{< relref "/ChapterFour/1100~1199/1170.Compare-Strings-by-Frequency-of-the-Smallest-Character.md" >}})|Medium||||60.8%|
132-
|1171|Remove Zero Sum Consecutive Nodes from Linked List|[Go]({{< relref "/ChapterFour/1100~1199/1171.Remove-Zero-Sum-Consecutive-Nodes-from-Linked-List.md" >}})|Medium||||42.0%|
132+
|1171|Remove Zero Sum Consecutive Nodes from Linked List|[Go]({{< relref "/ChapterFour/1100~1199/1171.Remove-Zero-Sum-Consecutive-Nodes-from-Linked-List.md" >}})|Medium||||42.1%|
133133
|1178|Number of Valid Words for Each Puzzle|[Go]({{< relref "/ChapterFour/1100~1199/1178.Number-of-Valid-Words-for-Each-Puzzle.md" >}})|Hard||||41.1%|
134134
|1189|Maximum Number of Balloons|[Go]({{< relref "/ChapterFour/1100~1199/1189.Maximum-Number-of-Balloons.md" >}})|Easy||||62.5%|
135135
|1202|Smallest String With Swaps|[Go]({{< relref "/ChapterFour/1200~1299/1202.Smallest-String-With-Swaps.md" >}})|Medium||||51.0%|

0 commit comments

Comments
 (0)