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() -- 从 ...
随机推荐
- 查看磁盘空间,Android各目录讲解
dfFilesystem Size Used Free Blksize/dev 2.0G 116.0K 2.0G 4096----------包含了所有Linux系统中使用的外部设备/sys/fs/c ...
- 【Swift后台】背景介绍
在2017年11月的时候,就已经对Swift后台进行过研究,简书上发表过相应文章,那时候发表的是单纯的对Vapor文档的翻译,此次则是作为进一步研究的学习笔记来保存. Swift后台的本质,主要是Va ...
- ActiveMQ入门操作示例
1. Queue 1.1 Producer 生产者:生产消息,发送端. 把jar包添加到工程中. 第一步:创建ConnectionFactory对象,需要指定服务端ip及端口号. 第二步:使用Conn ...
- axios表单提交,delete,get请求(待完善)
import { mapMutations} from 'vuex' import axios from 'axios' const mixins = { data() { return { } }, ...
- pipenv虚拟环境使用方法
1.打开cmd安装pipenv, pip install pipenv 2.新建工程目录,项目目录,然后cmd进入工程目录 基本命令: pipenv install ...
- 语义分割之车道线检测Lanenet(tensorflow版)
Lanenet 一个端到端的网络,包含Lanenet+HNet两个网络模型,其中,Lanenet完成对车道线的实例分割,HNet是一个小网络结构,负责预测变换矩阵H,使用转换矩阵H对同属一条车道线的所 ...
- (六)buildroot使用详解
为什么要使用buildroot? (文件系统搭建,强烈建议直接用buildroot,官网[http://buildroot.uclibc.org/]上有使用教程非常详细)文件系统通常要包含很多第三方软 ...
- (十三)Linux sysfs device_attribute
/*************************************************************************** * Linux sysfs device_at ...
- Go语言基础之Cookie和Session
Cookie和Session Cookie和Session是Web开发绕不开的一个环节,本文介绍了Cookie和Session的原理及在Go语言中如何操作Cookie. Cookie Cookie的由 ...
- Python tuple元组学习
1.tuple和list非常类似,但是tuple一旦初始化就不能修改 classmates = ('Michael', 'Bob', 'Tracy') 现在,classmates这个tuple不能变了 ...