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
相关推荐
YoungHong199224 分钟前
面试经典150题[074]:填充每个节点的下一个右侧节点指针 II(LeetCode 117)
leetcode·面试·职场和发展
RAY_010424 分钟前
Python—数据可视化pyecharts
开发语言·python
DanyHope31 分钟前
LeetCode 128. 最长连续序列:O (n) 时间的哈希集合 + 剪枝解法全解析
前端·leetcode·哈希算法·剪枝
元亓亓亓35 分钟前
LeetCode热题100--763. 划分字母区间--中等
算法·leetcode·职场和发展
Dream it possible!35 分钟前
LeetCode 面试经典 150_回溯_全排列(100_46_C++_中等)
c++·leetcode·面试·回溯
鹿角片ljp35 分钟前
力扣206.反转链表-双指针法(推荐)
算法·leetcode·链表
Lvan的前端笔记43 分钟前
python:用 dotenv 管理环境变量&生产环境怎么管理环境变量
网络·数据库·python
Java Fans43 分钟前
用PyQt打造带动画、碰撞检测和键盘控制的小游戏
python·计算机外设·pyqt
深蓝海拓44 分钟前
PySide6从0开始学习的笔记(十一) QSS 属性选择器
笔记·python·qt·学习·pyqt
AAA_bo11 小时前
liunx安装canda、python、nodejs、git,随后部署私有网页内容提取工具--JinaReader全攻略
linux·python·ubuntu·typescript·aigc·python3.11·jina