0384. Shuffle an Array
https://leetcode.com/problems/shuffle-an-array
Description
Given an integer array nums
, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.
Implement the Solution
class:
Solution(int[] nums)
Initializes the object with the integer arraynums
.int[] reset()
Resets the array to its original configuration and returns it.int[] shuffle()
Returns a random shuffling of the array.
Example 1:
Constraints:
1 <= nums.length <= 200
-106 <= nums[i] <= 106
All the elements of
nums
are unique.At most
5 * 104
calls in total will be made toreset
andshuffle
.
ac
random.nextInt()
is the key here.
Last updated