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:

  1. Input: 1
  2. Output: [7]

Example 2:

  1. Input: 2
  2. Output: [8,4]

Example 3:

  1. Input: 3
  2. 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

  1. class Solution {
  2. public:
  3. int rand10() {
  4. int ret = ;
  5. while(ret >= ) {
  6. ret = *(rand7()-) + rand7()-;
  7. }
  8. return ret%+;
  9. }
  10. };

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. php--常见算法2

    <?php function zhi($number){ $f1=1; $f2=1; for($i=3;$i<=$number;$i++){ //前一个的前一个值+前一个值 $f3=$f1 ...

  2. 第十章、logging模块

    目录 第十章.logging模块 一.logging模块及日志框架 第十章.logging模块 一.logging模块及日志框架 导入方式 import logging 作用 写日志 模块功能 # V ...

  3. 第八章·Logstash深入-通过TCP/UDP收集日志

    1.收集TCP/UDP日志 通过logstash的tcp/udp插件收集日志,通常用于在向elasticsearch日志补录丢失的部分日志,可以将丢失的日志通过一个TCP端口直接写入到elastics ...

  4. LVS (Linux虚拟服务器)-不同的负载均衡方法

    随着Internet用户的增长,基于Web的公司处理的通信量急剧增加.有各种解决方案来应对这种不断增长的流量. 一种解决方案是垂直扩展服务器(即:简单地向服务器添加更多的CPU和内存资源.)当然在一定 ...

  5. MFC的一些常用操作

    一.添加消息 MFC和win32不同的一点是MFC采用的是消息的映射机制,即每一个消息都和处理的函数做了映射,我们可以通过查找的方式来得到消息的对应的函数,当然MFC提供了一个非常简便的方法,我们通过 ...

  6. CentOS7 xrdp 安装和设置

    1) 安装 $ sudo yum install xrdp $ sudo yum install tigervnc $ sudo yum install tigervnc-server 2) 设置密码 ...

  7. springmvc后台获取表单提交的数据——@ModelAttribute等方式

    1.通过注解ModelAttribute直接映射表单中的参数到POJO.在from中的action写提交的路径,在input的name写参数的名称. package com.demo.model; p ...

  8. 12-SSMS图形化工具中不允许保存修改的解决办法

    1.报出的警告 2.解决办法 工具-->选项-->设计器--->表设计和数据库设计器-->阻止保存要求重新创建表的更改  的勾去掉就OK 了

  9. Kettle 事务、转换内顺序、excel模版、使用踩坑

    kettle中转换和作业的执行顺序:     1.一个作业内的转换,是顺序执行的.     2.一个转换内的步骤是并行执行的.     3.作业内不支持事务,转换内支持事务. 根据业务需要,通常需要在 ...

  10. 【原】eclipse连接数据库开发web项目

    之前也写过web项目,今天用的时候死活连不上My SQL,浪费了很多时间,下面总结一下: 在java项目里面访问数据库 (1)项目上右击->Build Path->add External ...