题目描述

已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数。

不要使用系统的 Math.random() 方法。

示例 1:

输入: 1
输出: [7]

示例 2:

输入: 2
输出: [8,4]

示例 3:

输入: 3
输出: [8,1,10]

提示:

  1. rand7 已定义。
  2. 传入参数: n 表示 rand10 的调用次数。

进阶:

  1. rand7()调用次数的 期望值 是多少 ?
  2. 你能否尽量少调用 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())的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 【LeetCode】470. Implement Rand10() Using Rand7() 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [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 ...

  5. 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 ...

  6. Java实现 LeetCode 470 用 Rand7() 实现 Rand10()

    470. 用 Rand7() 实现 Rand10() 已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数. 不要使用系 ...

  7. 470. Implement Rand10() Using Rand7() (拒绝采样Reject Sampling)

    1. 问题 已提供一个Rand7()的API可以随机生成1到7的数字,使用Rand7实现Rand10,Rand10可以随机生成1到10的数字. 2. 思路 简单说: (1)通过(Rand N - 1) ...

  8. leetcode 470. 用 Rand7() 实现 Rand10() (数学,优化策略)

    题目链接 https://leetcode-cn.com/problems/implement-rand10-using-rand7/ 题意: 给定一个rand7()的生成器,求解如何产生一个rand ...

  9. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

随机推荐

  1. 第二十五篇 jQuery 学习7 获取并设置 CSS 类

    jQuery 学习7 获取并设置 CSS 类   jQuery动态控制页面,那么什么是动态呢?我们就说一下静态,静态几乎又纯html+css完成,就是刷新页面之后,不会再出现什么变动,一个实打实的静态 ...

  2. python视频学习笔记6(名片管理系统开发)

    cards_main.py主函数 cards_tools.py -------------------------------------------------------------------- ...

  3. 在线预览(pptx、ppt、pps、docx、doc、xlsx、xls)

    http://view.officeapps.live.com/op/view.aspx?src=<文档位置> 示例文档https://www.dujin.org/file/ppt/duj ...

  4. beego中获取url以及参数的方式

    以下都全默认在controller下执行 获取当前请求的referer fmt.Println(this.Ctx.Request.Referer()) 输出:http://localhost:8080 ...

  5. java_day05_类和对象

    chap05目标:类和对象---------------------------------------------- 1.OOP特征概述 Java的编程语言是面向对象的,采用这种语言进行编程称为面向 ...

  6. Intellij IDEA导入java项目看不到左边的项目目录结构

    1 重新import项目 然后导入完成,就可以了,再不行的话,删除.idea文件,重新import整个Project

  7. go语言在Windows系统下编译成linux系统可执行文件

    package main import ( "fmt" "os" "os/exec" ) //filepath: 要编译的文件的路径 fun ...

  8. 解决myeclipse没有代码提示的问题

    今天和室友安装了一样的myeclipse版本,结果室友的自动提示功能有,我的输入“.”后却不能提示,这对我们敲代码简直来说是一个折磨,不能自动提示,本来还以为是系统问题,一个是win7,一个是win1 ...

  9. ngnix 配置说明

    #定义Nginx运行的用户和用户组 user www www; # #nginx进程数,建议设置为等于CPU总核心数. worker_processes ; # #全局错误日志定义类型,[ debug ...

  10. Java语言基础(9)

    1 方法(二) 1) 不带参数没有返回值的方法: 案例:Demo1 public class Demo1 { static void show(){ System.out.println(" ...