LC 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]
Runtime: 88 ms, faster than 42.31% of C++ online submissions for Implement Rand10() Using Rand7().
不会做,好题,是一道极限近似题。
https://leetcode.com/problems/implement-rand10-using-rand7/discuss/150301/Three-line-Java-solution-the-idea-can-be-generalized-to-%22Implement-RandM()-Using-RandN()%22
class Solution {
public:
int rand10() {
int ret = ;
while(ret >= ) {
ret = *(rand7()-) + rand7()-;
}
return ret%+;
}
};
LC 470. Implement Rand10() Using Rand7()的更多相关文章
- [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 ...
- 470. Implement Rand10() Using Rand7() (拒绝采样Reject Sampling)
1. 问题 已提供一个Rand7()的API可以随机生成1到7的数字,使用Rand7实现Rand10,Rand10可以随机生成1到10的数字. 2. 思路 简单说: (1)通过(Rand N - 1) ...
- 【LeetCode】470. Implement Rand10() Using Rand7() 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- LC 676. Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- [LC] 225. Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LC] 232. Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
随机推荐
- SQL 多表查询展示
########################多表########################SELECT COUNT(*) FROM MEMBER1 A; 查询出来的结果为43行数据: SEL ...
- 【转】通过lombok带你读透Builder构建器
原地址:https://www.jianshu.com/p/0d8fc3df3647?from=timeline&isappinstalled=0 很久之前,我在<effective j ...
- D - Yet Another Problem On a Subsequence CodeForces - 1000D (DP,组合数学)
D - Yet Another Problem On a Subsequence CodeForces - 1000D The sequence of integers a1,a2,-,aka1,a2 ...
- Linux之apt-get软件管理
apt-get 用Linux apt-get命令的第一步就是引入必需的软件库,Debian的软件库也就是所有Debian软件包的集合,它们存在互联网上的一些公共站点上.把它们的地址加入,apt-get ...
- UVa101 The Blocks Problem(不定长数组vector)
The Blocks Problem 书上的一道例题,代码思路比较清晰,可以看懂. 相关知识: 若a是一个vector,则: a.size():读取它的大小 a.resize():改变大小 a.pus ...
- 使用python读取配置文件并从mysql数据库中获取数据进行传参(基于Httprunner)
最近在使用httprunner进行接口测试,在传参时,用到了三种方法:(1)从csv文件中获取:(2)在config中声名然后进行引用:(3)从函数中获取.在测试过程中,往往有些参数是需要从数据库中获 ...
- 理论基础+实战控制台程序实现AutoFac注入
[半小时大话.net依赖注入](一)理论基础+实战控制台程序实现AutoFac注入 系列目录# 第一章|理论基础+实战控制台程序实现AutoFac注入 第二章|AutoFac的常见使用套路 第三章 ...
- li元素之间产生间隔
是因为li标签换行导致的 简单的解决办法是将所有的li标签写到一行(不过实际上一般不会这样做) 或者把ul设置font-size为0,但这样ul中的文字就会消失,所以要记得单独给子元素设置font-s ...
- Js之DateFormat工具类
/** * 对Date的扩展,将Date转化为指定格式的String * 年(y).季度(q).月(M).日(d).小时(h).分(m).秒(s)可以用1-2个占位符 * 示例: * FormatDa ...
- [旧版] CASthesis 模板编译的问题
写在前面的话:国科大最新版的模板在这里(https://github.com/mohuangrui/ucasthesis),利用它来写博后出站报告的相关介绍在这里: 本篇博文是针对另一个模板进行介绍的 ...