384. Shuffle an Array Medium

@problem@discussion
#Array#Math#Randomized



1/**
2 * [384] Shuffle an Array
3 *
4 * 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.
5 * Implement the Solution class:
6 * 
7 * 	Solution(int[] nums) Initializes the object with the integer array nums.
8 * 	int[] reset() Resets the array to its original configuration and returns it.
9 * 	int[] shuffle() Returns a random shuffling of the array.
10 * 
11 *  
12 * Example 1:
13 * 
14 * Input
15 * ["Solution", "shuffle", "reset", "shuffle"]
16 * [[[1, 2, 3]], [], [], []]
17 * Output
18 * [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
19 * Explanation
20 * Solution solution = new Solution([1, 2, 3]);
21 * solution.shuffle();    // Shuffle the array [1,2,3] and return its result.
22 *                        // Any permutation of [1,2,3] must be equally likely to be returned.
23 *                        // Example: return [3, 1, 2]
24 * solution.reset();      // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3]
25 * solution.shuffle();    // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2]
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	1 <= nums.length <= 50
31 * 	-10^6 <= nums[i] <= 10^6
32 * 	All the elements of nums are unique.
33 * 	At most 10^4 calls in total will be made to reset and shuffle.
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/shuffle-an-array/
39// discuss: https://leetcode.com/problems/shuffle-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43struct Solution {
44        false
45    }
46
47
48/** 
49 * `&self` means the method takes an immutable reference.
50 * If you need a mutable reference, change it to `&mut self` instead.
51 */
52impl Solution {
53
54    fn new(nums: Vec<i32>) -> Self {
55        
56    }
57    
58    fn reset(&self) -> Vec<i32> {
59        
60    }
61    
62    fn shuffle(&self) -> Vec<i32> {
63        
64    }
65}
66
67/**
68 * Your Solution object will be instantiated and called as such:
69 * let obj = Solution::new(nums);
70 * let ret_1: Vec<i32> = obj.reset();
71 * let ret_2: Vec<i32> = obj.shuffle();
72 */
73
74// submission codes end
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_384() {
82    }
83}
84


Back
© 2025 bowen.ge All Rights Reserved.