【Leetcode刷题】:Python:470. 用 Rand7() 实现 Rand10()

题目

470. 用 Rand7() 实现 Rand10()

题解:

https://leetcode-cn.com/problems/implement-rand10-using-rand7/solution/cong-zui-ji-chu-de-jiang-qi-ru-he-zuo-dao-jun-yun-/

代码:

# The rand7() API is already defined for you.
# def rand7():
# @return a random integer in the range 1 to 7

class Solution:
    def rand10(self):
        """
        :rtype: int
        """
        while True:
            nums = (rand7()-1)*7 + rand7()
            if nums <= 40:
                return nums % 10 + 1
上一篇:C#重构开关语句与空检查


下一篇:雪花算法生成一个id