Shuffle an Array

update Jan 25,2018 22:21

LeetCodearrow-up-right

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();

Basic Idea:

基本思路就是维持一个挡板,从右侧开始,选择每个数字,每次从挡板左边的数字中随机选择,然后swap,挡板左移。从右往左是为了生成随机数的时候方便(从0开始,而不是从k开始)。

  • Java Code: