[LeetCode] 470. Implement Rand10() Using Rand7()
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:
rand7is predefined.- Each testcase has one argument:
n, the number of times thatrand10is called.
Follow up:
- What is the
expected valuefor the number of calls torand7()function? - 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()的更多相关文章
- 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 ...
- 【LeetCode】470. Implement Rand10() Using Rand7() 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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()(Implement Rand10() Using Rand7())
题目描述 已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数. 不要使用系统的 Math.random() 方法. 示 ...
- [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 ...
- [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 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
随机推荐
- HDU2896病毒入侵AC_自动机
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- bzoj 2001 CITY 城市建设 cdq分治
题目传送门 题解: 对整个修改的区间进行分治.对于当前修改区间来说,我们对整幅图中将要修改的边权都先改成-inf,跑一遍最小生成树,然后对于一条树边并且他的权值不为-inf,那么这条边一定就是树边了. ...
- lightoj 1382 - The Queue(树形dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1382 题解:简单的树形dp加上组合数学. #include <iostr ...
- atcoder D - 11(组合数学)
题目链接:http://arc077.contest.atcoder.jp/tasks/arc077_b 题解:有n+1个数只有一个数字是有重复出现的,要求一共有多少不同的组合显然和这两个数的位置有关 ...
- hdu5491 The Next 模拟
Let LL denote the number of 1s in integer DD’s binary representation. Given two integers S1S1 and S2 ...
- 前端利器躬行记(4)——webpack进阶
webpack是一个非常强大的工具,除了前文所介绍的基础概念之外,还有各种进阶应用,例如Source Map.模块热替换.集成等,本文会对这些内容做依次讲解. 一. runtime和manifest ...
- Idea各种快捷生成Live Template的代码整合
Idea各种快捷生成整合 快速生成method方法注释 配置方法 打开Idea ---> Settings , 搜索 live 点击右边的 + 号,创建模板组 Template Group,之后 ...
- 【Nginx】 中的配置命令
一.location 1.1 概述 1.2 location的语法 1.3 Location正则案例 二.nginx rewrite 2.1 rewrite全局变量 2.2 判断IP地址来源 2.3 ...
- 【Offer】[48] 【最长不含重复字符的子字符串】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度.假设字符串中只包含'a'~'z'的字符.例如,在字符串&q ...
- git bash 界面修改成linux界面
在使用git bash操作git时,$符总是另起一行,给人感觉特别不爽,特别想修改成linux下一样的风格. 在git输入命令: vim ~/.bash_profile 进入insert模式,添加内容 ...