classSolution{ privateint[] nums; private Random random; publicSolution(int[] nums){ this.nums = nums; random = new Random(); } /** Resets the array to its original configuration and return it. */ publicint[] reset() { return nums; } /** Returns a random shuffling of the array. */ publicint[] shuffle() { int[] res = newint[nums.length]; HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) map.put(i, nums[i]); for (int i = 0; i < nums.length; i++){ int tmp = random.nextInt(nums.length); while (!map.containsKey(tmp)){ tmp = random.nextInt(nums.length); } res[i] = nums[tmp]; map.remove(tmp); } return res; } }
/** * 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(); */