Created
July 7, 2021 19:40
-
-
Save martende/d76659901d78276a16a49e41f05496ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "math/rand" | |
type Solution struct { | |
nums []int | |
init []int | |
} | |
func Constructor(nums []int) Solution { | |
init := make([]int,len(nums)) | |
for i := range init { | |
init[i] = i | |
} | |
return Solution{nums:nums,init: init} | |
} | |
/** 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 { | |
init := make([]int,len(this.nums)) | |
copy(init,this.nums) | |
/* | |
rand.Shuffle(len(init), func(i, j int) { | |
init[i], init[j] = init[j], init[i] | |
}) | |
*/ | |
for i := 0 ; i < len(init) - 1 ; i++ { | |
ri := i + rand.Intn(len(init)-i) | |
init[i],init[ri] = init[ri],init[i] | |
} | |
return init | |
} | |
/** | |
* Your Solution object will be instantiated and called as such: | |
* obj := Constructor(nums); | |
* param_1 := obj.Reset(); | |
* param_2 := obj.Shuffle(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment