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()的更多相关文章

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

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

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

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

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

  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. LC 676. Implement Magic Dictionary

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  8. [LC] 225. Implement Stack using Queues

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

  9. [LC] 232. Implement Queue using Stacks

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

随机推荐

  1. mysql 添加远程管理用户

    GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;   这一条是添加全权限的用户,用户名和密码 ...

  2. 使用js打印时去除页眉页脚

    写在前面 今天的开发遇到了使用window.print()功能进行当前页面打印的功能,因为页脚左边部分显示了url,这是不能存在的,已解决,写在这里. 正文 很多网上的方法都是不能用的,最后我找到一个 ...

  3. C++第四次作业--继承与派生

    C++ 继承 面向对象程序设计中最重要的一个概念是继承.继承允许我们依据另一个类来定义一个类,这使得创建和维护一个应用程序变得更容易.这样做,也达到了重用代码功能和提高执行效率的效果. 当创建一个类时 ...

  4. “美登杯”上海市高校大学生程序设计赛B. 小花梨的三角形(模拟,实现)

    题目链接:https://acm.ecnu.edu.cn/contest/173/problem/B/#report9 Problem B B . 小 花梨 的 三角形 时间限制:1000ms 空间限 ...

  5. LeetCode01 - 两数之和(Java 实现)

    LeetCode01 - 两数之和(Java 实现) 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/two-sum 题目描述 给定一个整数数组 ...

  6. dsoframer控件注册,解注册和检查注册情况

      public class DsoframerHelper { private static string dsoframerPath = System.Windows.Forms.Applicat ...

  7. context:component-scan 注解的扫描

    <context:component-scan base-package="com.matt.cloud"/> bean-context中 spring.handler ...

  8. React全家桶构建一款Web音乐App实战(六):排行榜及歌曲本地持久化

    上一节使用Redux管理歌曲相关数据,实现核心播放功能,播放功能是本项目最复杂的一个功能,涉及各个组件之间的数据交互,播放逻辑控制.这一节继续开发排行榜列表和排行榜详情,以及把播放歌曲和播放歌曲列表的 ...

  9. BZOJ 2946 [Poi2000]公共串 (二分+Hash/二分+后缀数组/后缀自动机)

    求多串的最长公共字串. 法1: 二分长度+hash 传送门 法2: 二分+后缀数组 传送门 法3: 后缀自动机 拿第一个串建自动机,然后用其他串在上面匹配.每次求出SAM上每个节点的最长匹配长度后,再 ...

  10. 【原】maven web项目eclipse搭建

    1.new->other->Maven Project,点击next 2.继续next 3.选择maven-archetype-webapp,如果找不到可以再Filter里面搜索web,就 ...