[LeetCode] 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:

rand7 is predefined.
Each testcase has one argument: n, the number of times that rand10 is called.

rand7 返回均匀分布的1到7,要求根据rand7 实现一个rand10, 要求返回均匀分布的1到10。
解决思路是先构建一个randN, N 要求是 10 的整数倍。由randN % 10 可以得到rand10。
由rand7 可以得到 rand49 , rand49 通过过滤掉大于等于40 的可以得到 rand40, 进而可以得到rand10。综上,解决思路如下:

rand7 -> rand49 -> rand40 -> rand10

此解决方案可以推广到所有randN 生成randM (N < M) 的场景。

C++ 实现

12345678910111213141516171819202122232425大专栏  [LeetCode] Implement Rand10() Using Rand7()s="line">2627
// int rand7();// @return a random integer in the range 1 to 7

class  {public:    int rand10() {        const int num = rand40();        return num % 10 + 1;    }private:    // return 0 ~ 48 randomly    int rand49()    {        return 7 * (rand7() - 1) + rand7() - 1;    }    // return 0 ~ 39 randomly    int rand40()    {        int num = rand49();        while(num >= 40)        {            num = rand49();        }        return num;    }};

原文地址:https://www.cnblogs.com/liuzhongrong/p/11874866.html

时间: 2024-07-30 12:01:13

[LeetCode] Implement Rand10() Using Rand7()的相关文章

[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 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:

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:

LeetCode470 - Implement Rand10() Using Rand7() - Medium (Python)

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:

[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: 1Output: [7]Example 2: In

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

问题 已提供一个Rand7()的API可以随机生成1到7的数字,使用Rand7实现Rand10,Rand10可以随机生成1到10的数字. 思路 简单说: (1)通过(Rand N - 1) % 10 + 1的方法,可以求出Rand10,当N是10的倍数的时候. (2)用( Rand7 - 1 ) * 7 + Rand7可以随机生成1-49,记作Rand49. (3)如果可以通过Rand49计算出Rand40,即随机生成1-40,就可以通过Rand40 % 10来取得Rand10. (4)如何通过

[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 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:

LeetCode: Implement strStr() [027]

[题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. [题意] 实现库函数strStr(), 功能是在字符串haystack中找出目标串needle第一次出现的索引位 [思路]字符串的匹配,能够用暴力解法,但不推荐.一般使用KMP算法求解. 简要介绍一下KMP的思想: haystack是

[LeetCode] Implement strStr() [18]

题目 Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 原题链接(点我) 解题思路 字符串匹配这也是个老题了,方法主要有下面4种, 1. 暴利破解法(BF),这个没啥说的,就是一轮一轮的比较,知道遇到相匹配的,这个的时间复杂度为O(n^2). 2. KMP,这应该是字符串匹配领域中最长听说的算

leetcode Implement Trie (Prefix Tree)

题目连接 https://leetcode.com/problems/implement-trie-prefix-tree/ Implement Trie (Prefix Tree) Description Implement a trie with insert, search, and startsWith methods. 字典树.. class TrieNode { public: // Initialize your data structure here. bool vis; Tri