mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-12 22:55:19 +08:00
Add solution 189
This commit is contained in:
@ -120,6 +120,8 @@
|
||||
|
||||
## 一. 目录
|
||||
|
||||
以下已经收录了 556 道题的题解,还有 13 道题在尝试优化到 beats 100%
|
||||
|
||||
| No. | Title | Solution | Acceptance | Difficulty | Frequency |
|
||||
|:--------:|:--------------------------------------------------------------|:--------:|:--------:|:--------:|:--------:|
|
||||
|0001|Two Sum|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0001.Two-Sum)|46.1%|Easy||
|
||||
@ -310,7 +312,7 @@
|
||||
|0186|Reverse Words in a String II||45.0%|Medium||
|
||||
|0187|Repeated DNA Sequences|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0187.Repeated-DNA-Sequences)|41.2%|Medium||
|
||||
|0188|Best Time to Buy and Sell Stock IV||29.2%|Hard||
|
||||
|0189|Rotate Array||36.3%|Medium||
|
||||
|0189|Rotate Array|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0189.Rotate-Array)|36.3%|Medium||
|
||||
|0190|Reverse Bits|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0190.Reverse-Bits)|41.5%|Easy||
|
||||
|0191|Number of 1 Bits|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0191.Number-of-1-Bits)|51.8%|Easy||
|
||||
|0192|Word Frequency||25.8%|Medium||
|
||||
|
@ -17,6 +17,8 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var try int
|
||||
|
||||
func main() {
|
||||
var (
|
||||
result []m.StatStatusPairs
|
||||
@ -40,7 +42,7 @@ func main() {
|
||||
// res, _ := json.Marshal(mdrows)
|
||||
//writeFile("leetcode_problem", res)
|
||||
mds := m.Mdrows{Mdrows: mdrows}
|
||||
res, err := readFile("./template.markdown", "{{.AvailableTable}}", mds)
|
||||
res, err := readFile("./template.markdown", "{{.AvailableTable}}", len(solutionIds), try, mds)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
@ -109,6 +111,7 @@ func loadSolutionsDir() []int {
|
||||
}
|
||||
}
|
||||
sort.Ints(solutionIds)
|
||||
try = len(files) - len(solutionIds)
|
||||
fmt.Printf("读取了 %v 道题的题解,当前目录下有 %v 个文件(可能包含 .DS_Store),有 %v 道题在尝试中\n", len(solutionIds), len(files), len(files)-len(solutionIds))
|
||||
return solutionIds
|
||||
}
|
||||
@ -153,7 +156,7 @@ func readTMPL(path string) string {
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func readFile(filePath, template string, mdrows m.Mdrows) ([]byte, error) {
|
||||
func readFile(filePath, template string, total, try int, mdrows m.Mdrows) ([]byte, error) {
|
||||
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -174,10 +177,14 @@ func readFile(filePath, template string, mdrows m.Mdrows) ([]byte, error) {
|
||||
newByte := reg.ReplaceAll(line, []byte(mdrows.AvailableTable()))
|
||||
output = append(output, newByte...)
|
||||
output = append(output, []byte("\n")...)
|
||||
} else if ok, _ := regexp.Match("{{.TotalNum}}", line); ok {
|
||||
reg := regexp.MustCompile("{{.TotalNum}}")
|
||||
newByte := reg.ReplaceAll(line, []byte(fmt.Sprintf("以下已经收录了 %v 道题的题解,还有 %v 道题在尝试优化到 beats 100%%", total, try)))
|
||||
output = append(output, newByte...)
|
||||
output = append(output, []byte("\n")...)
|
||||
} else {
|
||||
output = append(output, line...)
|
||||
output = append(output, []byte("\n")...)
|
||||
}
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
@ -120,6 +120,8 @@
|
||||
|
||||
## 一. 目录
|
||||
|
||||
{{.TotalNum}}
|
||||
|
||||
{{.AvailableTable}}
|
||||
|
||||
------------------------------------------------------------------
|
||||
|
24
leetcode/0189.Rotate-Array/189. Rotate Array.go
Normal file
24
leetcode/0189.Rotate-Array/189. Rotate Array.go
Normal file
@ -0,0 +1,24 @@
|
||||
package leetcode
|
||||
|
||||
// 解法一 时间复杂度 O(n),空间复杂度 O(1)
|
||||
func rotate(nums []int, k int) {
|
||||
k %= len(nums)
|
||||
reverse(nums)
|
||||
reverse(nums[:k])
|
||||
reverse(nums[k:])
|
||||
}
|
||||
|
||||
func reverse(a []int) {
|
||||
for i, n := 0, len(a); i < n/2; i++ {
|
||||
a[i], a[n-1-i] = a[n-1-i], a[i]
|
||||
}
|
||||
}
|
||||
|
||||
// 解法二 时间复杂度 O(n),空间复杂度 O(n)
|
||||
func rotate1(nums []int, k int) {
|
||||
newNums := make([]int, len(nums))
|
||||
for i, v := range nums {
|
||||
newNums[(i+k)%len(nums)] = v
|
||||
}
|
||||
copy(nums, newNums)
|
||||
}
|
50
leetcode/0189.Rotate-Array/189. Rotate Array_test.go
Normal file
50
leetcode/0189.Rotate-Array/189. Rotate Array_test.go
Normal file
@ -0,0 +1,50 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question189 struct {
|
||||
para189
|
||||
ans189
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para189 struct {
|
||||
nums []int
|
||||
k int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans189 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem189(t *testing.T) {
|
||||
|
||||
qs := []question189{
|
||||
|
||||
{
|
||||
para189{[]int{1, 2, 3, 4, 5, 6, 7}, 3},
|
||||
ans189{[]int{5, 6, 7, 1, 2, 3, 4}},
|
||||
},
|
||||
|
||||
{
|
||||
para189{[]int{-1, -100, 3, 99}, 2},
|
||||
ans189{[]int{3, 99, -1, -100}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 189------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans189, q.para189
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
rotate(p.nums, p.k)
|
||||
fmt.Printf("【output】:%v\n", p.nums)
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
75
leetcode/0189.Rotate-Array/README.md
Normal file
75
leetcode/0189.Rotate-Array/README.md
Normal file
@ -0,0 +1,75 @@
|
||||
# [189. Rotate Array](https://leetcode.com/problems/rotate-array/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array, rotate the array to the right by *k* steps, where *k* is non-negative.
|
||||
|
||||
**Follow up:**
|
||||
|
||||
- Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
|
||||
- Could you do it in-place with O(1) extra space?
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: nums = [1,2,3,4,5,6,7], k = 3
|
||||
Output: [5,6,7,1,2,3,4]
|
||||
Explanation:
|
||||
rotate 1 steps to the right: [7,1,2,3,4,5,6]
|
||||
rotate 2 steps to the right: [6,7,1,2,3,4,5]
|
||||
rotate 3 steps to the right: [5,6,7,1,2,3,4]
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: nums = [-1,-100,3,99], k = 2
|
||||
Output: [3,99,-1,-100]
|
||||
Explanation:
|
||||
rotate 1 steps to the right: [99,-1,-100,3]
|
||||
rotate 2 steps to the right: [3,99,-1,-100]
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= nums.length <= 2 * 10^4`
|
||||
- `-2^31 <= nums[i] <= 2^31 - 1`
|
||||
- `0 <= k <= 10^5`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 解法二,使用一个额外的数组,先将原数组下标为 i 的元素移动到 `(i+k) mod n` 的位置,再将剩下的元素拷贝回来即可。
|
||||
- 解法一,由于题目要求不能使用额外的空间,所以本题最佳解法不是解法二。翻转最终态是,末尾 `k mod n` 个元素移动至了数组头部,剩下的元素右移 `k mod n` 个位置至最尾部。确定了最终态以后再变换就很容易。先将数组中所有元素从头到尾翻转一次,尾部的所有元素都到了头部,然后再将 `[0,(k mod n) − 1]` 区间内的元素翻转一次,最后再将 `[k mod n, n − 1]` 区间内的元素翻转一次,即可满足题目要求。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
// 解法一 时间复杂度 O(n),空间复杂度 O(1)
|
||||
func rotate(nums []int, k int) {
|
||||
k %= len(nums)
|
||||
reverse(nums)
|
||||
reverse(nums[:k])
|
||||
reverse(nums[k:])
|
||||
}
|
||||
|
||||
func reverse(a []int) {
|
||||
for i, n := 0, len(a); i < n/2; i++ {
|
||||
a[i], a[n-1-i] = a[n-1-i], a[i]
|
||||
}
|
||||
}
|
||||
|
||||
// 解法二 时间复杂度 O(n),空间复杂度 O(n)
|
||||
func rotate1(nums []int, k int) {
|
||||
newNums := make([]int, len(nums))
|
||||
for i, v := range nums {
|
||||
newNums[(i+k)%len(nums)] = v
|
||||
}
|
||||
copy(nums, newNums)
|
||||
}
|
||||
```
|
75
website/content/ChapterFour/0189.Rotate-Array.md
Normal file
75
website/content/ChapterFour/0189.Rotate-Array.md
Normal file
@ -0,0 +1,75 @@
|
||||
# [189. Rotate Array](https://leetcode.com/problems/rotate-array/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array, rotate the array to the right by *k* steps, where *k* is non-negative.
|
||||
|
||||
**Follow up:**
|
||||
|
||||
- Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
|
||||
- Could you do it in-place with O(1) extra space?
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: nums = [1,2,3,4,5,6,7], k = 3
|
||||
Output: [5,6,7,1,2,3,4]
|
||||
Explanation:
|
||||
rotate 1 steps to the right: [7,1,2,3,4,5,6]
|
||||
rotate 2 steps to the right: [6,7,1,2,3,4,5]
|
||||
rotate 3 steps to the right: [5,6,7,1,2,3,4]
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: nums = [-1,-100,3,99], k = 2
|
||||
Output: [3,99,-1,-100]
|
||||
Explanation:
|
||||
rotate 1 steps to the right: [99,-1,-100,3]
|
||||
rotate 2 steps to the right: [3,99,-1,-100]
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= nums.length <= 2 * 10^4`
|
||||
- `-2^31 <= nums[i] <= 2^31 - 1`
|
||||
- `0 <= k <= 10^5`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 解法二,使用一个额外的数组,先将原数组下标为 i 的元素移动到 `(i+k) mod n` 的位置,再将剩下的元素拷贝回来即可。
|
||||
- 解法一,由于题目要求不能使用额外的空间,所以本题最佳解法不是解法二。翻转最终态是,末尾 `k mod n` 个元素移动至了数组头部,剩下的元素右移 `k mod n` 个位置至最尾部。确定了最终态以后再变换就很容易。先将数组中所有元素从头到尾翻转一次,尾部的所有元素都到了头部,然后再将 `[0,(k mod n) − 1]` 区间内的元素翻转一次,最后再将 `[k mod n, n − 1]` 区间内的元素翻转一次,即可满足题目要求。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
// 解法一 时间复杂度 O(n),空间复杂度 O(1)
|
||||
func rotate(nums []int, k int) {
|
||||
k %= len(nums)
|
||||
reverse(nums)
|
||||
reverse(nums[:k])
|
||||
reverse(nums[k:])
|
||||
}
|
||||
|
||||
func reverse(a []int) {
|
||||
for i, n := 0, len(a); i < n/2; i++ {
|
||||
a[i], a[n-1-i] = a[n-1-i], a[i]
|
||||
}
|
||||
}
|
||||
|
||||
// 解法二 时间复杂度 O(n),空间复杂度 O(n)
|
||||
func rotate1(nums []int, k int) {
|
||||
newNums := make([]int, len(nums))
|
||||
for i, v := range nums {
|
||||
newNums[(i+k)%len(nums)] = v
|
||||
}
|
||||
copy(nums, newNums)
|
||||
}
|
||||
```
|
@ -168,6 +168,7 @@ headless: true
|
||||
- [0174.Dungeon-Game]({{< relref "/ChapterFour/0174.Dungeon-Game.md" >}})
|
||||
- [0179.Largest-Number]({{< relref "/ChapterFour/0179.Largest-Number.md" >}})
|
||||
- [0187.Repeated-DNA-Sequences]({{< relref "/ChapterFour/0187.Repeated-DNA-Sequences.md" >}})
|
||||
- [0189.Rotate-Array]({{< relref "/ChapterFour/0189.Rotate-Array.md" >}})
|
||||
- [0190.Reverse-Bits]({{< relref "/ChapterFour/0190.Reverse-Bits.md" >}})
|
||||
- [0191.Number-of-1-Bits]({{< relref "/ChapterFour/0191.Number-of-1-Bits.md" >}})
|
||||
- [0198.House-Robber]({{< relref "/ChapterFour/0198.House-Robber.md" >}})
|
||||
|
Reference in New Issue
Block a user