classSolution { vector<int> nums; vector<int> result; public: Solution(vector<int> nums): nums(nums), result(nums) { } /** Resets the array to its original configuration and return it. */ vector<int> reset(){ result = nums; return result; } /** Returns a random shuffling of the array. */ vector<int> shuffle(){ int result_size = result.size(); for(int i = result_size-1; i >= 1; i--) { int now = rand() % (i+1); swap(result[i], result[i-now]); } return result; } };
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * vector<int> param_1 = obj.reset(); * vector<int> param_2 = obj.shuffle(); */
defreset(self): """ Resets the array to its original configuration and return it. :rtype: List[int] """ return self.nums
defshuffle(self): """ Returns a random shuffling of the array. :rtype: List[int] """ result = self.nums[:] result_size = len(result) for i in range(result_size): now = random.randint(i, result_size-1) result[i], result[now] = result[now], result[i] return result
# Your Solution object will be instantiated and called as such: # obj = Solution(nums) # param_1 = obj.reset() # param_2 = obj.shuffle()
func(this *Solution)Shuffle() []int { numsLen := len(this.nums) result := make([]int, numsLen) for i := 0; i < numsLen ; i++ { result[i] = this.nums[i] } for i := numsLen-1; i >= 1; i-- { now := rand.Intn(i+1) result[i], result[now] = result[now], result[i] } return result }
/** * Your Solution object will be instantiated and called as such: * obj := Constructor(nums); * param_1 := obj.Reset(); * param_2 := obj.Shuffle(); */