Java Solution with Index Shuffling and moving generated index to the bottom


  • 0
    E
    public class Solution {
        public static int nums[];
        public Solution(int[] nums) {
            this.nums=nums;
        }
        
        /** Resets the array to its original configuration and return it. */
        public int[] reset() {
            return nums;
        }
        
        /** Returns a random shuffling of the array. */
        public int[] shuffle() {
            Random rand= new Random();
            int arr[]=new int[nums.length];
            int num[]=nums.clone();
            int i=0,len=num.length;
            while(i<num.length){
                int index=rand.nextInt(len);
                arr[i++]=num[index];
                num[index]=num[len-1];
                len--;
            }
            return arr;
        }
    }
    
    /**
     * 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();
     */
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.