mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 19:53:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			49 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给定方法 <code>rand7</code> 可生成 <code>[1,7]</code> 范围内的均匀随机整数,试写一个方法 <code>rand10</code> 生成 <code>[1,10]</code> 范围内的均匀随机整数。</p>
 | 
						|
 | 
						|
<p>你只能调用 <code>rand7()</code> 且不能调用其他方法。请不要使用系统的 <code>Math.random()</code> 方法。</p>
 | 
						|
 | 
						|
<ol>
 | 
						|
</ol>
 | 
						|
 | 
						|
<p>每个测试用例将有一个内部参数 <code>n</code>,即你实现的函数 <code>rand10()</code> 在测试时将被调用的次数。请注意,这不是传递给 <code>rand10()</code> 的参数。</p>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
 | 
						|
<p><strong>示例 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>输入: </strong>1
 | 
						|
<strong>输出: </strong>[2]
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong>示例 2:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>输入: </strong>2
 | 
						|
<strong>输出: </strong>[2,8]
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong>示例 3:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>输入: </strong>3
 | 
						|
<strong>输出: </strong>[3,8,10]
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
 | 
						|
<p><strong>提示:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= n <= 10<sup>5</sup></code></li>
 | 
						|
</ul>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
 | 
						|
<p><strong>进阶:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>rand7()</code>调用次数的 <a href="https://en.wikipedia.org/wiki/Expected_value" target="_blank">期望值</a> 是多少 ?</li>
 | 
						|
	<li>你能否尽量少调用 <code>rand7()</code> ?</li>
 | 
						|
</ul>
 |