LeetCode 470. 用 Rand7() 实现 Rand10()(Implement Rand10() Using Rand7())
题目描述
已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数。
不要使用系统的 Math.random() 方法。
示例 1:
输入: 1
输出: [7]
示例 2:
输入: 2
输出: [8,4]
示例 3:
输入: 3
输出: [8,1,10]
提示:
rand7已定义。- 传入参数:
n表示rand10的调用次数。
进阶:
rand7()调用次数的 期望值 是多少 ?- 你能否尽量少调用
rand7()?
解题思路
用randN生成randM,其中N<M的基本思想是:
- 首先找到一个能覆盖M整数倍的随机序列,对于此题来说,最少能全覆盖的范围是1~49,即(rand7() - 1) * 7 + rand7(),调用此式子并舍去大于40的数,可以得到1~40的均匀分布
- 得到了1~40的均匀分布后,接着模10取余即可得到0~9的均匀分布,然后加1即是rand10
代码
// The rand7() API is already defined for you.
// int rand7();
// @return a random integer in the range 1 to 7 class Solution {
public:
int rand10() {
int r = (rand7() - ) * + rand7();
while(r > )
r = (rand7() - ) * + rand7();
return r % + ;
}
};
LeetCode 470. 用 Rand7() 实现 Rand10()(Implement Rand10() Using Rand7())的更多相关文章
- [Swift]LeetCode470. 用 Rand7() 实现 Rand10() | Implement Rand10() Using Rand7()
Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a functio ...
- [LeetCode] 470. Implement Rand10() Using Rand7()
Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a functio ...
- 【LeetCode】470. Implement Rand10() Using Rand7() 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Implement Rand10() Using Rand7() 使用Rand7()来实现Rand10()
Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a functio ...
- LC 470. Implement Rand10() Using Rand7()
Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a functio ...
- Java实现 LeetCode 470 用 Rand7() 实现 Rand10()
470. 用 Rand7() 实现 Rand10() 已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数. 不要使用系 ...
- 470. Implement Rand10() Using Rand7() (拒绝采样Reject Sampling)
1. 问题 已提供一个Rand7()的API可以随机生成1到7的数字,使用Rand7实现Rand10,Rand10可以随机生成1到10的数字. 2. 思路 简单说: (1)通过(Rand N - 1) ...
- leetcode 470. 用 Rand7() 实现 Rand10() (数学,优化策略)
题目链接 https://leetcode-cn.com/problems/implement-rand10-using-rand7/ 题意: 给定一个rand7()的生成器,求解如何产生一个rand ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
随机推荐
- 使用docker compose部署服务
谈到微服务的话题,技术上我们往往会涉及到多服务.多容器的部署与管理. Docker 有三个主要的作用:Build, Ship和Run.使用docker compose我们可以在Run的层面解决很多实际 ...
- 开源you-get项目爬虫,以及基于python+selenium的自动测试利器
写在前面 爬虫和自动测试,对于python来说是最合适不过也是最擅长的. 开源的项目也很多,例如you-get项目https://github.com/soimort/you-get.盗链和爬虫神器. ...
- SQL语句复习【专题六】
SQL语句复习[专题六] 用户 + 创建表 --创建一个新的用户,然后给新的用户创建一张表,然后给表中添加一些数据.查询表中的数据 --创建用户需要比较大的权限 DBA create user di ...
- Spinner simpleAdapte适配器 下拉列表
public class MainActivity extends AppCompatActivity { private TextView text; private Spinner spinner ...
- Linux基础命令01
绝对路径:从“/”根目录下开始 常用的一些命令选项 ls ls :使用方式 ls {空格}选项 {空格}参数 (全写为list)(等同于dir) 列出当前目录下所有的文件,包括隐藏文件 ls -a ...
- contos7 yum 安装golang
一.安装 [root@localhost golang]# yum install golang 安装默认目录为/usr/lib/golang/ 二.配置环境变量 echo "export ...
- zencart后台管理中选项名称和选项内容和属性控制页面出错解决办法 WARNING: An Error occurred, please refresh the page and try again
后台管理中选项名称和选项内容和属性控制出现以下错误的解决办法WARNING: An Error occurred, please refresh the page and try again zen ...
- CSS——字体大小最常用的单位
px (像素): 将像素的值赋予给你的文本.这是一个绝对单位, 它导致了在任何情况下,页面上的文本所计算出来的像素值都是一样的. ems: 1em 等于我们设计的当前元素的父元素上设置的字体大小 (更 ...
- HttpRunner完整笔记(从搭建到应用)
一.安装 (1) 直接python2 –m pip install HttpRunner,安装完成后,可使用hrun –V 来查看安装版本:python2 pip install -U H ...
- (八)zabbix获取到的数值自定义单位
1) 查找php文件 # find / -name "func.inc.php" /usr/share/zabbix/include/func.inc.php 2)修改文件 #vi ...