mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
add: leetcode 0384 solution
This commit is contained in:
28
leetcode/0384.Shuffle-an-Array/384.Shuffle an Array.go
Normal file
28
leetcode/0384.Shuffle-an-Array/384.Shuffle an Array.go
Normal file
@ -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
|
||||
}
|
Reference in New Issue
Block a user