当前位置:网站首页>470. Rand10() is implemented with rand7()
470. Rand10() is implemented with rand7()
2022-04-23 17:32:00 【hequnwang10】
One 、 Title Description
Given method rand7 Generative [1,7] Uniform random integer in range , Try to write a method rand10 Generate [1,10] Uniform random integer in range .
You can only call rand7() And you can't call other methods . Please do not use the system's Math.random() Method .
Each test case will have an internal parameter n, That is, the function you implement rand10() The number of times it will be called during the test . Please note that , This is not passed on to rand10() Parameters of .
Example 1:
Input : 1
Output : [2]
Example 2:
Input : 2
Output : [2,8]
Example 3:
Input : 3
Output : [3,8,10]
Two 、 Problem solving
Reject sampling
/** * The rand7() API is already defined in the parent class SolBase. * public int rand7(); * @return a random integer in the range 1 to 7 */
class Solution extends SolBase {
public int rand10() {
int maxNum = Integer.MAX_VALUE;
while(maxNum > 40){
maxNum = rand7() + (rand7()-1) * 7;
}
return 1+maxNum%10;
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009467.html
边栏推荐
- [C#] 彻底搞明白深拷贝
- Deep understanding of control inversion and dependency injection
- HCIP第五次实验
- In embedded system, must the program code in flash be moved to ram to run?
- ClickHouse-表引擎
- 基于51单片机红外无线通讯仿真
- Using quartz under. Net core - [1] quick start
- Clickhouse SQL operation
- 開期貨,開戶雲安全還是相信期貨公司的軟件?
- Construction of functions in C language programming
猜你喜欢
线性代数感悟之2
Compare the performance of query based on the number of paging data that meet the query conditions
ClickHouse-表引擎
SiteServer CMS5. 0 Usage Summary
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
Use of todesk remote control software
Double pointer advanced -- leetcode title -- container with the most water
线性代数感悟之1
PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
Clickhouse table engine
随机推荐
Future 用法详解
[ES6] promise related (event loop, macro / micro task, promise, await / await)
Shell-awk命令的使用
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
Use of todesk remote control software
flink 学习(十二)Allowed Lateness和 Side Output
C listens for WMI events
1-3 components and modules
Header built-in object
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
In embedded system, must the program code in flash be moved to ram to run?
Understanding and small examples of unity3d object pool
198. 打家劫舍-动态规划
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
How does matlab draw the curve of known formula and how does excel draw the function curve image?
ClickHouse-数据类型
C# Task. Delay and thread The difference between sleep
Baidu Map 3D rotation and tilt angle adjustment
1-2 JSX syntax rules
Basic case of Baidu map