Python | Leetcode Python题解之第470题用Rand7()实现Rand10()

题目:

题解:

python 复制代码
class Solution:
    def rand10(self) -> int:
        while True:
            a = rand7()
            b = rand7()
            idx = (a - 1) * 7 + b
            if idx <= 40:
                return 1 + (idx - 1) % 10
            a = idx - 40
            b = rand7()
            # get uniform dist from 1 - 63
            idx = (a - 1) * 7 + b
            if idx <= 60:
                return 1 + (idx - 1) % 10
            a = idx - 60
            b = rand7()
            # get uniform dist from 1 - 21
            idx = (a - 1) * 7 + b
            if idx <= 20:
                return 1 + (idx - 1) % 10
相关推荐
AI蜗牛之家30 分钟前
Qwen系列之Qwen3解读:最强开源模型的细节拆解
人工智能·python
whyeekkk1 小时前
python打卡第48天
开发语言·python
chengooooooo2 小时前
leetcode Top100 238. 除自身以外数组的乘积|数组系列
算法·leetcode
Eiceblue3 小时前
Python读取PDF:文本、图片与文档属性
数据库·python·pdf
weixin_527550403 小时前
初级程序员入门指南
javascript·python·算法
程序员的世界你不懂4 小时前
Appium+python自动化(十)- 元素定位
python·appium·自动化
CryptoPP4 小时前
使用WebSocket实时获取印度股票数据源(无调用次数限制)实战
后端·python·websocket·网络协议·区块链
树叶@5 小时前
Python数据分析7
开发语言·python
老胖闲聊6 小时前
Python Rio 【图像处理】库简介
开发语言·图像处理·python
GalaxyPokemon6 小时前
LeetCode - 53. 最大子数组和
算法·leetcode·职场和发展