[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 ...
随机推荐
- CF 1206D - Shortest Cycle Floyd求最小环
Shortest Cycle 题意 有n(n <= 100000)个数字,两个数字间取&运算结果大于0的话连一条边.问图中的最小环. 思路 可以发现当非0数的个数很大,比如大于200时, ...
- lightoj 1046 - Rider(bfs)
A rider is a fantasy chess piece that can jump like a knight several times in a single move. A rider ...
- Gym 101964 题解
B:Broken Watch (别问,问就是队友写的) 代码: import java.awt.List; import java.io.BufferedInputStream; import jav ...
- js中的this介绍
今天跟大家一起简单的来了解一下js中一个有趣的东西,this. 在js中我们用面向对象的思想去编写的时候,各个模块之间的变量就不那么容易获取的到了,当然也可以通过闭包的方式拿到其他函数的变量,如果说每 ...
- python 中的while循环、格式化、编码初始
while循环 循环:不断重复着某件事就是循环 while 关键字 死循环:while True: 循环体 while True: # 死循环# print("坚强")# pr ...
- 工作中遇到的99%SQL优化,这里都能给你解决方案(三)
-- 示例表 CREATE TABLE `employees` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(24) NOT NULL ...
- MySQL性能调优与架构设计(简朝阳)
https://www.cnblogs.com/crazylqy/category/625963.html
- 使用php安装pcntl模块
想添加个php多进程任务,突然发现服务器上php不支持pcntl扩展,再看了下也没有phpize这个模块 首先看下当前php版本 [root@htest ~]# php -v PHP 5.3.3 (c ...
- mapper文件中“添加一条新数据并返回此数据的ID(主键)”的方法
在mapper文件的insert语句前加上<selectKey>标签即可 如下: 添加前测试: 添加后测试:
- 面试官:服务器安装 JDK 还是 JRE?可以只安装 JRE 吗?
前些日子有知友面试时被问到如题所示的问题,由于他之前没有准备到这些最最基础的知识,没有考虑过这个问题,所以被问到时竟一脸萌币,回答的不是很好.这道题主要考的是对 Java 基础知识的了解,有些同学可能 ...