classSolution{ privateint[] ori; privateint[] nums; publicSolution(int[] nums){ this.nums = nums; this.ori = nums.clone(); } /** Resets the array to its original configuration and return it. */ publicint[] reset() { returnthis.ori; } /** Returns a random shuffling of the array. */ publicint[] shuffle() { Random random = new Random(); int length = nums.length; for(int i = length - 1; i > 1; i--) { int j = random.nextInt(i + 1); int temp = nums[j]; nums[j] = nums[i]; nums[i] = temp; } return nums; } }
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int[] param_1 = obj.reset(); * int[] param_2 = obj.shuffle(); */
python的话,import random也可以达到相同效果:
1 2 3 4 5
defshuffle(self): for i in range(len(self.output)): j = random.randint(i, len(self.output) - 1) self.output[i], self.output[j] = self.output[j], self.output[i] return self.output