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();
实现shuffle和reset方法,分别可以完成数组的随机打乱和还原。随机打乱即该数组中元素的全部排列组合结果都可以以等比例的几率输出。java
直观的思路来讲,咱们会将数组复制一份,并根据数组的长度来生成随机数,并得到该随机数下标上的值放在新的位置上。本文将详细讲解一下网上的另外一种解法,即Fisher–Yates
算法,该算法可以用O(n)的时间随机打乱一个数组。算法
该算法的步骤以下:数组
重复以上步骤,直到数组第一个元素。dom
下面解释一下证实,即为什么每一个该结果是等几率的排列组合结果。this
第一步操做,对于数组中全部的元素,均有1/n的几率交换到最后一个位置上code
第二步操做能够分为两种场景。it
((n-1)/n) * (1/n-1) = 1/n
((n-1)/n * (1/n-1)) = 1/n
第三步操做能够分为三种场景:io
(1 - 1/n - (n-1)/n * 1 / (n-1)) = (n-2) / n
, 所以最终几率为(n-2)/n * 1/(n-2) = 1/n
(1 - 1/n - (n-1)/n * 1 / (n-1)) = (n-2) / n
, 所以最终几率为(n-2)/n * 1/(n-2) = 1/n
1/n
综上,这种方法可以保证每个元素能够等几率出如今任何位置上。代码以下:class
private int[] nums; private Random random; public Solution(int[] nums) { this.nums = nums; random = new Random(); } /** Resets the array to its original configuration and return it. */ public int[] reset() { return this.nums; } /** Returns a random shuffling of the array. */ public int[] shuffle() { if(nums == null) return null; int[] result = nums.clone(); for(int j = 1; j < result.length; j++) { int i = random.nextInt(j + 1); swap(result, i, j); } return result; } private void swap(int[] a, int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; }