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() -- 从 ...
随机推荐
- 【ExtJs】ext前台中的日期控件传输时间到后台的转换保存过程
//前台日期选择框 {fieldLabel:, padding: ',afterLabelTextTpl: required,allowBlank: false,format: 'Y-m-d H:i: ...
- Access to XMLHttpRequest at 'http://localhost:8090/user/getotp' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
ajax跨域请求错误 解决: springboot中: 加注解 @CrossOrigin
- javascript是否像php一样有isset和empty?
javascript是否像php一样有isset和empty? is set()在php中用于检测是否设置了变量木浴桶,函数返回布尔值true/false.在javascript中,您可以用替换它!( ...
- Nginx如何配置防盗链
配置要点 none : 允许没有http_refer的请求访问资源: blocked : 允许不是http://开头的,不带协议的请求访问资源: 119.28.190.215 start.igrow. ...
- hbase shell 基本操作
hbase shell 基本操作 启动HBASE [hadoop@master ~]$hbase shell 2019-01-24 13:53:59,990 WARN [main] ut ...
- Solved: XXX esx.problem.hyperthreading.unmitigated.formatOnHost not found XXX
esxi 出现XXX esx.problem.hyperthreading.unmitigated.formatOnHost not found XXX 问题. 回避方法: 将高级设置-->Us ...
- C#中构建多线程应用程序[转] ----代码示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- centos redis自启动
#!/bin/sh # chkconfig: 2345 90 10 # description: Redis is a persistent key-value database # Simple R ...
- Spark Submit给jar包中的main函数传递参数
1 示范 spark-submit --master xxx demo.jar "arg1" "arg2" 运行的jar包和传参放在最后,就可以了
- ARM cortex-version
cortex-M\A\R M microcontroller 微控制器 就是单片机 A application 应用及处理器 就是手机平板电脑等 R realtime 实时处理器 响应 ...