diff --git a/leetcode/301-400/0384.Shuffle-an-Array/README.md b/leetcode/301-400/0384.Shuffle-an-Array/README.md index 1b57d93ac..cb9b3f00f 100644 --- a/leetcode/301-400/0384.Shuffle-an-Array/README.md +++ b/leetcode/301-400/0384.Shuffle-an-Array/README.md @@ -1,28 +1,31 @@ # [384.Shuffle an Array][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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. -**Example 1:** - -``` -Input: a = "11", b = "1" -Output: "100" -``` +Implement the `Solution` class: -## 题意 -> ... +- `Solution(int[] nums)` Initializes the object with the integer array `nums`. +- `int[] reset()` Resets the array to its original configuration and returns it. +- `int[] shuffle()` Returns a random shuffling of the array. -## 题解 +**Example 1:** -### 思路1 -> ... -Shuffle an Array -```go ``` - +Input +["Solution", "shuffle", "reset", "shuffle"] +[[[1, 2, 3]], [], [], []] +Output +[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] + +Explanation +Solution solution = new Solution([1, 2, 3]); +solution.shuffle(); // Shuffle the array [1,2,3] and return its result. + // Any permutation of [1,2,3] must be equally likely to be returned. + // Example: return [3, 1, 2] +solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] +solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] +``` ## 结语 diff --git a/leetcode/301-400/0384.Shuffle-an-Array/Solution.go b/leetcode/301-400/0384.Shuffle-an-Array/Solution.go index d115ccf5e..38bdc1793 100644 --- a/leetcode/301-400/0384.Shuffle-an-Array/Solution.go +++ b/leetcode/301-400/0384.Shuffle-an-Array/Solution.go @@ -1,5 +1,46 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "math/rand" + "time" +) + +type Shuffle struct { + origin []int + r *rand.Rand +} + +func Constructor(nums []int) Shuffle { + return Shuffle{ + origin: nums, + r: rand.New(rand.NewSource(time.Now().UnixNano())), + } +} + +func (this *Shuffle) Reset() []int { + this.r = rand.New(rand.NewSource(time.Now().UnixNano())) + return this.origin +} + +func (this *Shuffle) Shuffle() []int { + n := len(this.origin) + cur := make([]int, len(this.origin)) + copy(cur, this.origin) + for i := n - 1; i > 0; i-- { + j := this.r.Intn(i + 1) + cur[i], cur[j] = cur[j], cur[i] + } + return cur +} +func Solution(nums []int, op []string) [][]int { + c := Constructor(nums) + var ret [][]int + for _, name := range op { + if name == "shuffle" { + ret = append(ret, c.Shuffle()) + continue + } + ret = append(ret, c.Reset()) + } + return ret } diff --git a/leetcode/301-400/0384.Shuffle-an-Array/Solution_test.go b/leetcode/301-400/0384.Shuffle-an-Array/Solution_test.go index 14ff50eb4..5d6a52681 100644 --- a/leetcode/301-400/0384.Shuffle-an-Array/Solution_test.go +++ b/leetcode/301-400/0384.Shuffle-an-Array/Solution_test.go @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + op []string + expect [][]int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1}, []string{"shuffle", "rest"}, [][]int{{1}, {1}}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.op) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.op) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }