当前位置:网站首页>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
边栏推荐
- Deep understanding of control inversion and dependency injection
- [difference between Oracle and MySQL]
- Compare the performance of query based on the number of paging data that meet the query conditions
- 1-2 JSX syntax rules
- ASP. Net core reads the configuration file in the class library project
- Self use learning notes - connected and non connected access to database
- 1217_使用SCons生成目标文件
- How to sort the numbers with text in Excel from small to large instead of the first number
- 圆环回原点问题-字节跳动高频题
- 嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
猜你喜欢

XTask与Kotlin Coroutine的使用对比

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
![[WPF binding 3] listview basic binding and data template binding](/img/2e/fbdb4175297bb4964a8ccfd0b909ae.png)
[WPF binding 3] listview basic binding and data template binding

Perception of linear algebra 2

Halo 开源项目学习(二):实体类与数据表

Double pointer advanced -- leetcode title -- container with the most water

Future 用法详解

为什么有些人说单片机简单,我学起来这么吃力?

双闭环直流调速系统matlab/simulink仿真

PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
随机推荐
基于51单片机红外无线通讯仿真
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
XTask与Kotlin Coroutine的使用对比
In ancient Egypt and Greece, what base system was used in mathematics
Self use learning notes - connected and non connected access to database
Further optimize Baidu map data visualization
为什么有些人说单片机简单,我学起来这么吃力?
ASP. Net core configuration options (Part 2)
Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
[simple understanding of database]
JS to find the character that appears three times in the string
The system cannot be started after AHCI is enabled
Manually implement call, apply and bind functions
C dapper basically uses addition, deletion, modification and query transactions, etc
Clickhouse table engine
El date picker limits the selection range from the current time to two months ago
Summary of common SQL statements
Header built-in object
Come out after a thousand calls