Skip to content

Commit 1bc9d34

Browse files
committed
Add solution 189
1 parent c700a33 commit 1bc9d34

File tree

8 files changed

+240
-4
lines changed

8 files changed

+240
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@
120120

121121
## 一. 目录
122122

123+
以下已经收录了 556 道题的题解,还有 13 道题在尝试优化到 beats 100%
124+
123125
| No. | Title | Solution | Acceptance | Difficulty | Frequency |
124126
|:--------:|:--------------------------------------------------------------|:--------:|:--------:|:--------:|:--------:|
125127
|0001|Two Sum|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0001.Two-Sum)|46.1%|Easy||
@@ -310,7 +312,7 @@
310312
|0186|Reverse Words in a String II||45.0%|Medium||
311313
|0187|Repeated DNA Sequences|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0187.Repeated-DNA-Sequences)|41.2%|Medium||
312314
|0188|Best Time to Buy and Sell Stock IV||29.2%|Hard||
313-
|0189|Rotate Array||36.3%|Medium||
315+
|0189|Rotate Array|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0189.Rotate-Array)|36.3%|Medium||
314316
|0190|Reverse Bits|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0190.Reverse-Bits)|41.5%|Easy||
315317
|0191|Number of 1 Bits|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0191.Number-of-1-Bits)|51.8%|Easy||
316318
|0192|Word Frequency||25.8%|Medium||

automation/render.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"strings"
1818
)
1919

20+
var try int
21+
2022
func main() {
2123
var (
2224
result []m.StatStatusPairs
@@ -40,7 +42,7 @@ func main() {
4042
// res, _ := json.Marshal(mdrows)
4143
//writeFile("leetcode_problem", res)
4244
mds := m.Mdrows{Mdrows: mdrows}
43-
res, err := readFile("./template.markdown", "{{.AvailableTable}}", mds)
45+
res, err := readFile("./template.markdown", "{{.AvailableTable}}", len(solutionIds), try, mds)
4446
if err != nil {
4547
fmt.Println(err)
4648
return
@@ -109,6 +111,7 @@ func loadSolutionsDir() []int {
109111
}
110112
}
111113
sort.Ints(solutionIds)
114+
try = len(files) - len(solutionIds)
112115
fmt.Printf("读取了 %v 道题的题解,当前目录下有 %v 个文件(可能包含 .DS_Store),有 %v 道题在尝试中\n", len(solutionIds), len(files), len(files)-len(solutionIds))
113116
return solutionIds
114117
}
@@ -153,7 +156,7 @@ func readTMPL(path string) string {
153156
return string(data)
154157
}
155158

156-
func readFile(filePath, template string, mdrows m.Mdrows) ([]byte, error) {
159+
func readFile(filePath, template string, total, try int, mdrows m.Mdrows) ([]byte, error) {
157160
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
158161
if err != nil {
159162
return nil, err
@@ -174,10 +177,14 @@ func readFile(filePath, template string, mdrows m.Mdrows) ([]byte, error) {
174177
newByte := reg.ReplaceAll(line, []byte(mdrows.AvailableTable()))
175178
output = append(output, newByte...)
176179
output = append(output, []byte("\n")...)
180+
} else if ok, _ := regexp.Match("{{.TotalNum}}", line); ok {
181+
reg := regexp.MustCompile("{{.TotalNum}}")
182+
newByte := reg.ReplaceAll(line, []byte(fmt.Sprintf("以下已经收录了 %v 道题的题解,还有 %v 道题在尝试优化到 beats 100%%", total, try)))
183+
output = append(output, newByte...)
184+
output = append(output, []byte("\n")...)
177185
} else {
178186
output = append(output, line...)
179187
output = append(output, []byte("\n")...)
180188
}
181189
}
182-
return output, nil
183190
}

automation/template.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@
120120

121121
## 一. 目录
122122

123+
{{.TotalNum}}
124+
123125
{{.AvailableTable}}
124126

125127
------------------------------------------------------------------
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode
2+
3+
// 解法一 时间复杂度 O(n),空间复杂度 O(1)
4+
func rotate(nums []int, k int) {
5+
k %= len(nums)
6+
reverse(nums)
7+
reverse(nums[:k])
8+
reverse(nums[k:])
9+
}
10+
11+
func reverse(a []int) {
12+
for i, n := 0, len(a); i < n/2; i++ {
13+
a[i], a[n-1-i] = a[n-1-i], a[i]
14+
}
15+
}
16+
17+
// 解法二 时间复杂度 O(n),空间复杂度 O(n)
18+
func rotate1(nums []int, k int) {
19+
newNums := make([]int, len(nums))
20+
for i, v := range nums {
21+
newNums[(i+k)%len(nums)] = v
22+
}
23+
copy(nums, newNums)
24+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question189 struct {
9+
para189
10+
ans189
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para189 struct {
16+
nums []int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans189 struct {
23+
one []int
24+
}
25+
26+
func Test_Problem189(t *testing.T) {
27+
28+
qs := []question189{
29+
30+
{
31+
para189{[]int{1, 2, 3, 4, 5, 6, 7}, 3},
32+
ans189{[]int{5, 6, 7, 1, 2, 3, 4}},
33+
},
34+
35+
{
36+
para189{[]int{-1, -100, 3, 99}, 2},
37+
ans189{[]int{3, 99, -1, -100}},
38+
},
39+
}
40+
41+
fmt.Printf("------------------------Leetcode Problem 189------------------------\n")
42+
43+
for _, q := range qs {
44+
_, p := q.ans189, q.para189
45+
fmt.Printf("【input】:%v ", p)
46+
rotate(p.nums, p.k)
47+
fmt.Printf("【output】:%v\n", p.nums)
48+
}
49+
fmt.Printf("\n\n\n")
50+
}

leetcode/0189.Rotate-Array/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [189. Rotate Array](https://leetcode.com/problems/rotate-array/)
2+
3+
## 题目
4+
5+
Given an array, rotate the array to the right by *k* steps, where *k* is non-negative.
6+
7+
**Follow up:**
8+
9+
- Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
10+
- Could you do it in-place with O(1) extra space?
11+
12+
**Example 1:**
13+
14+
```
15+
Input: nums = [1,2,3,4,5,6,7], k = 3
16+
Output: [5,6,7,1,2,3,4]
17+
Explanation:
18+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
19+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
20+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: nums = [-1,-100,3,99], k = 2
27+
Output: [3,99,-1,-100]
28+
Explanation:
29+
rotate 1 steps to the right: [99,-1,-100,3]
30+
rotate 2 steps to the right: [3,99,-1,-100]
31+
```
32+
33+
**Constraints:**
34+
35+
- `1 <= nums.length <= 2 * 10^4`
36+
- `-2^31 <= nums[i] <= 2^31 - 1`
37+
- `0 <= k <= 10^5`
38+
39+
## 题目大意
40+
41+
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
42+
43+
## 解题思路
44+
45+
- 解法二,使用一个额外的数组,先将原数组下标为 i 的元素移动到 `(i+k) mod n` 的位置,再将剩下的元素拷贝回来即可。
46+
- 解法一,由于题目要求不能使用额外的空间,所以本题最佳解法不是解法二。翻转最终态是,末尾 `k mod n` 个元素移动至了数组头部,剩下的元素右移 `k mod n` 个位置至最尾部。确定了最终态以后再变换就很容易。先将数组中所有元素从头到尾翻转一次,尾部的所有元素都到了头部,然后再将 `[0,(k mod n) − 1]` 区间内的元素翻转一次,最后再将 `[k mod n, n − 1]` 区间内的元素翻转一次,即可满足题目要求。
47+
48+
## 代码
49+
50+
```go
51+
package leetcode
52+
53+
// 解法一 时间复杂度 O(n),空间复杂度 O(1)
54+
func rotate(nums []int, k int) {
55+
k %= len(nums)
56+
reverse(nums)
57+
reverse(nums[:k])
58+
reverse(nums[k:])
59+
}
60+
61+
func reverse(a []int) {
62+
for i, n := 0, len(a); i < n/2; i++ {
63+
a[i], a[n-1-i] = a[n-1-i], a[i]
64+
}
65+
}
66+
67+
// 解法二 时间复杂度 O(n),空间复杂度 O(n)
68+
func rotate1(nums []int, k int) {
69+
newNums := make([]int, len(nums))
70+
for i, v := range nums {
71+
newNums[(i+k)%len(nums)] = v
72+
}
73+
copy(nums, newNums)
74+
}
75+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [189. Rotate Array](https://leetcode.com/problems/rotate-array/)
2+
3+
## 题目
4+
5+
Given an array, rotate the array to the right by *k* steps, where *k* is non-negative.
6+
7+
**Follow up:**
8+
9+
- Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
10+
- Could you do it in-place with O(1) extra space?
11+
12+
**Example 1:**
13+
14+
```
15+
Input: nums = [1,2,3,4,5,6,7], k = 3
16+
Output: [5,6,7,1,2,3,4]
17+
Explanation:
18+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
19+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
20+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: nums = [-1,-100,3,99], k = 2
27+
Output: [3,99,-1,-100]
28+
Explanation:
29+
rotate 1 steps to the right: [99,-1,-100,3]
30+
rotate 2 steps to the right: [3,99,-1,-100]
31+
```
32+
33+
**Constraints:**
34+
35+
- `1 <= nums.length <= 2 * 10^4`
36+
- `-2^31 <= nums[i] <= 2^31 - 1`
37+
- `0 <= k <= 10^5`
38+
39+
## 题目大意
40+
41+
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
42+
43+
## 解题思路
44+
45+
- 解法二,使用一个额外的数组,先将原数组下标为 i 的元素移动到 `(i+k) mod n` 的位置,再将剩下的元素拷贝回来即可。
46+
- 解法一,由于题目要求不能使用额外的空间,所以本题最佳解法不是解法二。翻转最终态是,末尾 `k mod n` 个元素移动至了数组头部,剩下的元素右移 `k mod n` 个位置至最尾部。确定了最终态以后再变换就很容易。先将数组中所有元素从头到尾翻转一次,尾部的所有元素都到了头部,然后再将 `[0,(k mod n) − 1]` 区间内的元素翻转一次,最后再将 `[k mod n, n − 1]` 区间内的元素翻转一次,即可满足题目要求。
47+
48+
## 代码
49+
50+
```go
51+
package leetcode
52+
53+
// 解法一 时间复杂度 O(n),空间复杂度 O(1)
54+
func rotate(nums []int, k int) {
55+
k %= len(nums)
56+
reverse(nums)
57+
reverse(nums[:k])
58+
reverse(nums[k:])
59+
}
60+
61+
func reverse(a []int) {
62+
for i, n := 0, len(a); i < n/2; i++ {
63+
a[i], a[n-1-i] = a[n-1-i], a[i]
64+
}
65+
}
66+
67+
// 解法二 时间复杂度 O(n),空间复杂度 O(n)
68+
func rotate1(nums []int, k int) {
69+
newNums := make([]int, len(nums))
70+
for i, v := range nums {
71+
newNums[(i+k)%len(nums)] = v
72+
}
73+
copy(nums, newNums)
74+
}
75+
```

website/content/menu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ headless: true
168168
- [0174.Dungeon-Game]({{< relref "/ChapterFour/0174.Dungeon-Game.md" >}})
169169
- [0179.Largest-Number]({{< relref "/ChapterFour/0179.Largest-Number.md" >}})
170170
- [0187.Repeated-DNA-Sequences]({{< relref "/ChapterFour/0187.Repeated-DNA-Sequences.md" >}})
171+
- [0189.Rotate-Array]({{< relref "/ChapterFour/0189.Rotate-Array.md" >}})
171172
- [0190.Reverse-Bits]({{< relref "/ChapterFour/0190.Reverse-Bits.md" >}})
172173
- [0191.Number-of-1-Bits]({{< relref "/ChapterFour/0191.Number-of-1-Bits.md" >}})
173174
- [0198.House-Robber]({{< relref "/ChapterFour/0198.House-Robber.md" >}})

0 commit comments

Comments
 (0)