8000 :sparkles: Added solution to 189 · ninefive/awesome-golang-leetcode@8b2f096 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b2f096

Browse files
author
Sathish Babu
committed
✨ Added solution to 189
1 parent aec3a37 commit 8b2f096

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

src/0189.Rotate-Array/README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,33 @@
55
66
## Description
77

8+
Given an array, rotate the array to the right by k steps, where k is non-negative.
9+
10+
Follow up:
11+
12+
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
13+
Could you do it in-place with O(1) extra space?
14+
15+
816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: nums = [1,2,3,4,5,6,7], k = 3
20+
Output: [5,6,7,1,2,3,4]
21+
Explanation:
22+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
23+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
24+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: nums = [-1,-100,3,99], k = 2
31+
Output: [3,99,-1,-100]
32+
Explanation:
33+
rotate 1 steps to the right: [99,-1,-100,3]
34+
rotate 2 steps to the right: [3,99,-1,-100]
1335
```
1436

1537
## 题意

src/0189.Rotate-Array/Solution.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) {
4+
n := len(nums)
5+
if k == 0 || k%n == 0 {
6+
return
7+
}
8+
k = k % n
9+
reverse(nums, 0, n-1)
10+
reverse(nums, 0, k-1)
11+
reverse(nums, k, n-1)
12+
}
13+
14+
func reverse(arr []int, x, y int) {
15+
for i, j := x, y; i <= j; i, j = i+1, j-1 {
16+
arr[i], arr[j] = arr[j], arr[i]
17+
}
518
}

src/0189.Rotate-Array/Solution_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,25 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
k int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", []int{1, 2, 3, 4, 5, 6, 7}, 3, []int{5, 6, 7, 1, 2, 3, 4}},
18+
{"TestCase", []int{1, 2, 3, 4, 5, 6, 7}, 7, []int{1, 2, 3, 4, 5, 6, 7}},
19+
{"TestCase", []int{1, 2, 3, 4, 5, 6, 7}, 21, []int{1, 2, 3, 4, 5, 6, 7}},
20+
{"TestCase", []int{1, 2, 3, 4, 5, 6, 7}, 6, []int{2, 3, 4, 5, 6, 7, 1}},
21+
{"TestCase", []int{1, 2, 3, 4, 5, 6, 7}, 20, []int{2, 3, 4, 5, 6, 7, 1}},
1922
}
2023

2124
// 开始测试
2225
for i, c := range cases {
2326
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25-
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
tmp := c.nums
28+
Solution(c.nums, c.k)
29+
if !reflect.DeepEqual(c.nums, c.expect) {
30+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
31+
c.expect, c.nums, tmp, c.k)
2832
}
2933
})
3034
}

0 commit comments

Comments
 (0)
0