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:

Input: 2
Output: [8,4]

Example 3:

Input: 3
Output: [8,1,10]

思路:题意比较简单,用rand7()来实现rand10()。rand7()产生的是1到7所有的整数,rand10()我们希望产生的是1到10所有的整数。有一点比较明显的是,如果我们只call一次rand7(),我们是不可能产生8,9,10三个数字。所以显然我们应该至少call 2次。call 2次会产生的数字范围是1-49,注意不是1-14. (是乘法不是加法)1-49中如果我们可以discard 41-49. 那么此时我们拥有的是1-40,可以产生题目要求的uniform random。因此题目要求转变为如果产生1-40,在输出答案的是把这个产生的随机数和10取余之后,要再加1. 不然40%10=0. 
class Solution:
    def rand10(self):
        """
        :rtype: int
        """
        rand = 41

        while rand >= 41:
            rand = (rand7()-1) * 7 + rand7() 

        return rand%10 +1

原文地址:https://www.cnblogs.com/sky37/p/12244492.html

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

LeetCode470 - Implement Rand10() Using Rand7() - Medium (Python)的相关文章

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:

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

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

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

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)如何通过

LeetCode 28 Implement strStr() (C,C++,Java,Python)

Problem: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If

136. Single Number [medium] (Python)

题目链接 https://leetcode.com/problems/single-number/ 题目原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using ex

338. Counting Bits [medium] (Python)

题目链接 https://leetcode.com/problems/counting-bits/ 题目原文 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example: For num = 5