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
相关推荐
特立独行的猫A几秒前
13MB 单文件、一条命令变服务:把仓颉写的 cjh 内嵌进你的应用,当它的 AI 大脑
java·后端·python
deepseek239 分钟前
Agent任务中途断线,怎么避免重复执行?SQLite检查点实测
python·sqlite·任务调度·幂等性·ai agent
2401_868534781 小时前
【网络安全
网络·python
文丑颜不良啊12 小时前
LeetCode1941~1960题目解析
java·leetcode
❁满城风絮*12 小时前
Claude Code 学习总结
python·agent·claude
维克兜率天14 小时前
【维克】复合型CTA:趋势+套利的双轮驱动
python·学习方法
伞伞悦读14 小时前
【第76期】两张3行表左连接变5行:主键、validate与未匹配排查完整教程
人工智能·python
用户83562907805115 小时前
使用 Python 在 Excel 中添加和自定义迷你图
后端·python
用户83562907805115 小时前
使用 Python 在 Excel 图表中添加趋势线与回归方程
后端·python
特立独行的猫A15 小时前
一份 Markdown 发十个平台:cspub 一稿多投
java·后端·python