Shuffle an Array

Shuffle a set of numbers without duplicates.

Example

// Init an array with set 1, 2, and 3.
int[] nums = {1,2,3};
Solution solution = new Solution(nums);

// Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.
solution.shuffle();

// Resets the array back to its original configuration [1,2,3].
solution.reset();

// Returns the random shuffling of array [1,2,3].
solution.shuffle();

题意:给定一个数组,实现两个接口 reset() 和 shuffle(), 前者为置位返回初始的数组,后者为随机化数组。

思路:用swap,每次从[i,n-1]中随机一个数,和第i个数交换即可。

public class Solution {
    int[] original;
    int[] shuffled;  
    Random r;

    public Solution(int[] nums) {
        original = nums;
        shuffled = Arrays.copyOf(nums,nums.length);
        r = new Random();
    }

    /** Resets the array to its original configuration and return it. */
    public int[] reset() {
        shuffled=Arrays.copyOf(original, original.length);
        return shuffled;
    }

    /** Returns a random shuffling of the array. */
    public int[] shuffle() {
        int len = shuffled.length;

        for(int i=0; i<len; i++){
            int si = r.nextInt(len-i);
            int temp = shuffled[i];
            shuffled[i]=shuffled[si+i];
            shuffled[si+i]=temp;
        }

        return shuffled;
    }
}

results matching ""

    No results matching ""