|
| 1 | +<h2><a href="https://leetcode.com/problems/shuffle-an-array/">384. Shuffle an Array</a></h2><h3>Medium</h3><hr><div><p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> |
| 2 | + |
| 3 | +<p>Implement the <code>Solution</code> class:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> |
| 7 | + <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> |
| 8 | + <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> |
| 9 | +</ul> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | + |
| 14 | +<pre><strong>Input</strong> |
| 15 | +["Solution", "shuffle", "reset", "shuffle"] |
| 16 | +[[[1, 2, 3]], [], [], []] |
| 17 | +<strong>Output</strong> |
| 18 | +[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] |
| 19 | + |
| 20 | +<strong>Explanation</strong> |
| 21 | +Solution solution = new Solution([1, 2, 3]); |
| 22 | +solution.shuffle(); // Shuffle the array [1,2,3] and return its result. |
| 23 | + // Any permutation of [1,2,3] must be equally likely to be returned. |
| 24 | + // Example: return [3, 1, 2] |
| 25 | +solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] |
| 26 | +solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] |
| 27 | + |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong>Constraints:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li><code>1 <= nums.length <= 50</code></li> |
| 35 | + <li><code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code></li> |
| 36 | + <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> |
| 37 | + <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> |
| 38 | +</ul> |
| 39 | +</div> |
0 commit comments