simple java solution


  • 11
    2
    public class Solution {
    
        private 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() {
            int[] rand = new int[nums.length];
            for (int i = 0; i < nums.length; i++){
                int r = (int) (Math.random() * (i+1));
                rand[i] = rand[r];
                rand[r] = nums[i];
            }
            return rand;
        }
    }
    
    /**
     * 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();
     */
    

  • 1

    Though it is still under the hood of Knuth Shuffle, your solution is concise, elegant and without a explicit swap function. Nice!


  • 0
    A

    amazing solution


Log in to reply
 

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