From 469e775a97c3db432803743fab6fc7a76a31e62f Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Mon, 22 Nov 2021 16:50:43 +0800 Subject: [PATCH] add: leetcode 0384 solution --- .../384.Shuffle an Array.go | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 leetcode/0384.Shuffle-an-Array/384.Shuffle an Array.go diff --git a/leetcode/0384.Shuffle-an-Array/384.Shuffle an Array.go b/leetcode/0384.Shuffle-an-Array/384.Shuffle an Array.go new file mode 100644 index 00000000..54190842 --- /dev/null +++ b/leetcode/0384.Shuffle-an-Array/384.Shuffle an Array.go @@ -0,0 +1,28 @@ +package leetcode + +import "math/rand" + +type Solution struct { + nums []int +} + +func Constructor(nums []int) Solution { + return Solution{ + nums: nums, + } +} + +/** Resets the array to its original configuration and return it. */ +func (this *Solution) Reset() []int { + return this.nums +} + +/** Returns a random shuffling of the array. */ +func (this *Solution) Shuffle() []int { + arr := make([]int, len(this.nums)) + copy(arr, this.nums) + rand.Shuffle(len(arr), func(i, j int) { + arr[i], arr[j] = arr[j], arr[i] + }) + return arr +}