mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			39 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你一个整数数组 <code>nums</code> ,设计算法来打乱一个没有重复元素的数组。打乱后,数组的所有排列应该是 <strong>等可能</strong> 的。</p>
 | 
						||
 | 
						||
<p>实现 <code>Solution</code> class:</p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>Solution(int[] nums)</code> 使用整数数组 <code>nums</code> 初始化对象</li>
 | 
						||
	<li><code>int[] reset()</code> 重设数组到它的初始状态并返回</li>
 | 
						||
	<li><code>int[] shuffle()</code> 返回数组随机打乱后的结果</li>
 | 
						||
</ul>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>示例 1:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入</strong>
 | 
						||
["Solution", "shuffle", "reset", "shuffle"]
 | 
						||
[[[1, 2, 3]], [], [], []]
 | 
						||
<strong>输出</strong>
 | 
						||
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
 | 
						||
 | 
						||
<strong>解释</strong>
 | 
						||
Solution solution = new Solution([1, 2, 3]);
 | 
						||
solution.shuffle();    // 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。例如,返回 [3, 1, 2]
 | 
						||
solution.reset();      // 重设数组到它的初始状态 [1, 2, 3] 。返回 [1, 2, 3]
 | 
						||
solution.shuffle();    // 随机返回数组 [1, 2, 3] 打乱后的结果。例如,返回 [1, 3, 2]
 | 
						||
</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>1 <= nums.length <= 50</code></li>
 | 
						||
	<li><code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code></li>
 | 
						||
	<li><code>nums</code> 中的所有元素都是 <strong>唯一的</strong></li>
 | 
						||
	<li>最多可以调用 <code>10<sup>4</sup></code> 次 <code>reset</code> 和 <code>shuffle</code></li>
 | 
						||
</ul>
 |