当前位置:网站首页>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
边栏推荐
- Further optimize Baidu map data visualization
- Simulation of infrared wireless communication based on 51 single chip microcomputer
- Deep understanding of control inversion and dependency injection
- C语言程序设计之函数的构造
- EF core in ASP Generate core priority database based on net entity model
- Further study of data visualization
- 394. 字符串解码-辅助栈
- Input file upload
- Open futures, open an account, cloud security or trust the software of futures companies?
- [C] thoroughly understand the deep copy
猜你喜欢
[ES6] promise related (event loop, macro / micro task, promise, await / await)
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
01 - get to know the advantages of sketch sketch
In embedded system, must the program code in flash be moved to ram to run?
1217_使用SCons生成目标文件
【WPF绑定3】 ListView基础绑定和数据模板绑定
双闭环直流调速系统matlab/simulink仿真
2. Electron's HelloWorld
C语言函数详解
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
随机推荐
剑指 Offer 03. 数组中重复的数字
2. Electron's HelloWorld
Compare the performance of query based on the number of paging data that meet the query conditions
Self use learning notes - connectingstring configuration
Promise (I)
Generating access keys using JSON webtoken
386. 字典序排数(中等)-迭代-全排列
Shell-sed命令的使用
C语言程序设计之函数的构造
JVM类加载机制
flink 学习(十二)Allowed Lateness和 Side Output
基于51单片机红外无线通讯仿真
PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
Summary of common SQL statements
[ES6] promise related (event loop, macro / micro task, promise, await / await)
为什么有些人说单片机简单,我学起来这么吃力?
JS failed to change all variables and changed to the return method. Finally, the problem was solved
Why do some people say SCM is simple and I have to learn it so hard?
Shell-入门、变量、以及基本的语法
C dapper basically uses addition, deletion, modification and query transactions, etc