shuffle-an-array

https://leetcode.com/problems/shuffle-an-array/

public class Solution {

    int [] origNums;
    int [] shufNums;
    java.util.Random rd;

    public Solution(int[] nums) {
        origNums = nums;
        shufNums = origNums.clone();
        rd = new java.util.Random();
    }
    
    /** Resets the array to its original configuration and return it. */
    public int[] reset() {
        return origNums;
    }
    
    /** Returns a random shuffling of the array. */
    public int[] shuffle() {
        for (int i=shufNums.length-1; i>0; i--) {
            int k = Math.abs(rd.nextInt() % (i+1));
            int tmp = shufNums[k];
            shufNums[k] = shufNums[i];
            shufNums[i] = tmp;
        }
        return shufNums;
    }
}

/**
 * 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();
 */
原文地址:https://www.cnblogs.com/charlesblc/p/5815225.html