classSolution{ int[] origins; int[] shuffled; publicSolution(int[] nums){ origins = Arrays.copyOf(nums, nums.length); shuffled= Arrays.copyOf(nums, nums.length); } /** Resets the array to its original configuration and return it. */ publicint[] reset() { return (int[]) origins.clone(); } /** Returns a random shuffling of the array. */ publicint[] shuffle() { Random r = new Random(); int n = shuffled.length; int[] ans = newint[n]; int k = 0; while(n > 0) { int randomIndex = r.nextInt(n); ans[k++] = shuffled[randomIndex]; // swap shuffled[randomIndex] and shuffled[n]. // because next iteration will randomly select a value from 0 to n-1 int tmp = shuffled[n-1]; shuffled[n-1] = shuffled[randomIndex]; shuffled[randomIndex] = tmp; --n; } return ans; } }
/** * 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(); */