Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a function rand10 which generates a uniform random integer in the range 1 to 10.

Do NOT use system's Math.random().

Example 1:

Input: 1
Output: [7]

Example 2:

Input: 2

Output: [8,4]

Example 3:

Input: 3
Output: [8,1,10]

Note:

  1. rand7 is predefined.
  2. Each testcase has one argument: n, the number of times that rand10 is called.

Follow up:

  1. What is the expected value for the number of calls to rand7() function?
  2. Could you minimize the number of calls to rand7()?

Analyse

使用rand7模拟出rand10

用多的模拟少的很简单,比如如果是要rand10模拟rand7,调用一次rand10,如果结果>7则无效,再一次rand10,直到结果<=7即可

第一种办法,把1-10分成两部分,这样就可以用rand7来模拟,总共需要>=2次rand7

第一次rand7,用来区分生成1-5还是6-10,如果rand7的返回值 > 4,生成6-10,< 4,生成1-5

第二次rand7,模拟rand5,要生成6-10则结果+5

1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10
int leftOrRight() {
int tmp = rand7();
if (tmp < 4)
{
return 0;
}
else if (tmp > 4)
{
return 1;
}
else
{
return leftOrRight();
}
} int getSmall5() {
int tmp = rand7();
if (tmp <= 5)
{
return tmp;
}
else
{
return getSmall5();
}
} int rand10() {
int lr = leftOrRight();
if (lr == 0)
{
return getSmall5();
}
else
{
return getSmall5() + 5;
}
}

这种方法性能很差,为了拿到符合要求的数字甚至使用了两次递归

Runtime: 288 ms, faster than 7.28% of C++ online submissions for Implement Rand10() Using Rand7().

Memory Usage: 9.7 MB, less than 80.00% of C++ online submissions for Implement Rand10() Using Rand7().

第二种办法,来自leetcode,用两次rand7,建立一个二维坐标与1-10的映射,

   1  2  3  4  5  6  7
--------------------
1| 1 2 3 4 5 6 7
2| 8 9 10 1 2 3 4
3| 5 6 7 8 9 10 1
4| 2 3 4 5 6 7 8
5| 9 10 1 2 3 4 5
6| 6 7 8 9 10 * *
7| * * * * * * *
int rand10()
{
int row, col, index = 0;
do
{
col = rand7();
row = rand7();
index = col + (row - 1) * 7;
}
while (index > 40); return 1 + index % 10;
}

简化一下代码

int rand10() {
int index = rand7() + (rand7() - 1) * 7;
while (index > 40)
{
index = rand7() + (rand7() - 1) * 7;
} return 1 + index % 10;
}

[LeetCode] 470. Implement Rand10() Using Rand7()的更多相关文章

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

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

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

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

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

  4. LeetCode 470. 用 Rand7() 实现 Rand10()(Implement Rand10() Using Rand7())

    题目描述 已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数. 不要使用系统的 Math.random() 方法. 示 ...

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

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

  7. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

    翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...

  8. [LeetCode] 225. Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

随机推荐

  1. 洛谷P2577 [ZJOI2005]午餐 打饭时间作为容量DP

    P2577 [ZJOI2005]午餐 )逼着自己做DP 题意: 有n个人打饭,每个人都有打饭时间和吃饭时间.有两个打饭窗口,问如何安排可以使得总用时最少. 思路: 1)可以发现吃饭时间最长的要先打饭. ...

  2. 美团2018年CodeM大赛-资格赛 分数 暴力模拟

    链接:https://www.nowcoder.com/acm/contest/138/D来源:牛客网 小胖参加了人生中最重要的比赛——MedoC资格赛.MedoC的资格赛由m轮构成,使用常见的“加权 ...

  3. HDU 1087 Super Jumping! Jumping! Jumping! 最长递增子序列(求可能的递增序列的和的最大值) *

    Super Jumping! Jumping! Jumping! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64 ...

  4. 【LeetCode】[0002] 【两数之和】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 给出两个非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果 ...

  5. Redis真的那么好用吗

    Redis是什么 Redis是一个开源的底层使用C语言编写的key-value存储数据库.可用于缓存.事件发布订阅.高速队列等场景.而且支持丰富的数据类型:string(字符串).hash(哈希).l ...

  6. @Qualifier高级应用---按类别批量依赖注入【享学Spring】

    每篇一句 罗斯:选秀状元可能有水货,但MVP绝对没有 前言 在上篇文章(讲解@LoadBalanced负载均衡)的末尾,我抛出了一个很重要的问题,建议小伙伴自己深入思考一番:本文主要针对此问题,作出一 ...

  7. 互联网寒冬之泪:Android开发程序员,你够优秀吗?

    我想每个开发者在学习成长的过程中,在面临技术难题的时候,都有经历过自我怀疑的过程,但是有时候这并不是你的错,大家都经历过如此的过程.我们作为一个开发者,在成长的过程中,总有一些小的胜利和小的沮丧,学着 ...

  8. 松软科技课堂:Winform之TextBox

    松软科技文(www.sysoft.net.cn): 文本框的几种模式:Multiline(多行).PasswordChar(密码)将文本框的PasswordChar设为*就是密码框效果,将MultiL ...

  9. 2019-2020-1 20199314 <Linux内核原理与分析>第二周作业

    1.基础学习内容 1.1 冯诺依曼体系结构 计算机由控制器.运算器.存储器.输入设备.输出设备五部分组成. 1.1.1 冯诺依曼计算机特点 (1)采用存储程序方式,指令和数据不加区别混合存储在同一个存 ...

  10. C++基础之泛型算法

    标准库并未给每个容器添加大量功能,因此,通过大量泛型算法,来弥补.这些算法大多数独立于任何特定的容器,且是通用的,可用于不同类型的容器和不同的元素. 迭代器使得算法不依赖容器,但是算法依赖于元素的类型 ...