Shuffle an Array
Design a class over an integer array that supports two operations: reset() restores and returns the array in its original order, and shuffle() returns a uniformly random permutation of the array in which every possible ordering is equally likely.
Open official problem prompt ↗Produce a random reordering of an array where all n! orderings are equally probable, plus the ability to snap back to the starting order.
Shuffling a deck by repeatedly picking one card at random from the cards you have not yet placed and laying it down next — every card is equally likely to end up in any position.
- Input
- operations = ["Solution", "shuffle", "reset", "shuffle"], args = [[[1,2,3]], [], [], []]
- Output
- [null, [3,1,2], [1,2,3], [3,2,1]]
- Why
- shuffle returns a random permutation of [1,2,3], reset restores the original [1,2,3], and the next shuffle produces another random permutation.
1 <= nums.length <= 50-10^6 <= nums[i] <= 10^6All elements of nums are uniqueAt most 10^4 calls total to reset and shuffle