Skip to content

Commit 338c489

Browse files
committed
Add solution and test-cases for problem 384
1 parent f03226b commit 338c489

File tree

3 files changed

+72
-29
lines changed

3 files changed

+72
-29
lines changed

leetcode/301-400/0384.Shuffle-an-Array/README.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [384.Shuffle an Array][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an integer array `nums`, design an algorithm to randomly shuffle the array. All permutations of the array should be **equally likely** as a result of the shuffling.
75

8-
**Example 1:**
9-
10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
6+
Implement the `Solution` class:
147

15-
## 题意
16-
> ...
8+
- `Solution(int[] nums)` Initializes the object with the integer array `nums`.
9+
- `int[] reset()` Resets the array to its original configuration and returns it.
10+
- `int[] shuffle()` Returns a random shuffling of the array.
1711

18-
## 题解
12+
**Example 1:**
1913

20-
### 思路1
21-
> ...
22-
Shuffle an Array
23-
```go
2414
```
25-
15+
Input
16+
["Solution", "shuffle", "reset", "shuffle"]
17+
[[[1, 2, 3]], [], [], []]
18+
Output
19+
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
20+
21+
Explanation
22+
Solution solution = new Solution([1, 2, 3]);
23+
solution.shuffle(); // Shuffle the array [1,2,3] and return its result.
24+
// Any permutation of [1,2,3] must be equally likely to be returned.
25+
// Example: return [3, 1, 2]
26+
solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3]
27+
solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2]
28+
```
2629

2730
## 结语
2831

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

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"math/rand"
5+
"time"
6+
)
7+
8+
type Shuffle struct {
9+
origin []int
10+
r *rand.Rand
11+
}
12+
13+
func Constructor(nums []int) Shuffle {
14+
return Shuffle{
15+
origin: nums,
16+
r: rand.New(rand.NewSource(time.Now().UnixNano())),
17+
}
18+
}
19+
20+
func (this *Shuffle) Reset() []int {
21+
this.r = rand.New(rand.NewSource(time.Now().UnixNano()))
22+
return this.origin
23+
}
24+
25+
func (this *Shuffle) Shuffle() []int {
26+
n := len(this.origin)
27+
cur := make([]int, len(this.origin))
28+
copy(cur, this.origin)
29+
for i := n - 1; i > 0; i-- {
30+
j := this.r.Intn(i + 1)
31+
cur[i], cur[j] = cur[j], cur[i]
32+
}
33+
return cur
34+
}
35+
func Solution(nums []int, op []string) [][]int {
36+
c := Constructor(nums)
37+
var ret [][]int
38+
for _, name := range op {
39+
if name == "shuffle" {
40+
ret = append(ret, c.Shuffle())
41+
continue
42+
}
43+
ret = append(ret, c.Reset())
44+
}
45+
return ret
546
}

leetcode/301-400/0384.Shuffle-an-Array/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
op []string
15+
expect [][]int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1}, []string{"shuffle", "rest"}, [][]int{{1}, {1}}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.inputs, c.op)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.inputs, c.op)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)