当前位置:网站首页>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
边栏推荐
- Halo 开源项目学习(二):实体类与数据表
- Manually implement call, apply and bind functions
- 198. 打家劫舍-动态规划
- JVM类加载机制
- Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
- C listens for WMI events
- Shell - introduction, variables, and basic syntax
- Low code development platform sorting
- Devexpress GridView add select all columns
- In ancient Egypt and Greece, what base system was used in mathematics
猜你喜欢
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
Deep understanding of control inversion and dependency injection
XTask与Kotlin Coroutine的使用对比
1217_使用SCons生成目标文件
PC电脑使用无线网卡连接上手机热点,为什么不能上网
Future 用法详解
Learning record of uni app dark horse yougou project (Part 2)
Qt 修改UI没有生效
Perception of linear algebra 2
C# Task. Delay and thread The difference between sleep
随机推荐
SiteServer CMS5. 0 Usage Summary
For the space occupation of the software, please refer to the installation directory
Promise (II)
Shell-入门、变量、以及基本的语法
Allowed latency and side output
Net standard
How to change input into text
1217_使用SCons生成目标文件
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
[difference between Oracle and MySQL]
1-2 JSX syntax rules
Entity Framework core captures database changes
In ancient Egypt and Greece, what base system was used in mathematics
How to use the input table one-way service to send (occupy less) picture files (body transmission)? FileReader built-in object involved
Use of five routing guards
为什么有些人说单片机简单,我学起来这么吃力?
Perception of linear algebra 2
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
Use of shell awk command