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
相关推荐
Freak嵌入式7 分钟前
MicroPython LVGL基础知识和概念:时序与动态效果
开发语言·python·github·php·gui·lvgl·micropython
zhangzeyuaaa41 分钟前
Python 中的 Map 和 Reduce 详解
开发语言·python
abant21 小时前
leetcode 239 单调队列 需要一些记忆
算法·leetcode·职场和发展
七夜zippoe1 小时前
Java技术未来展望:GraalVM、Quarkus、Helidon等新趋势探讨
java·开发语言·python·quarkus·graaivm·helidon
m0_738120721 小时前
网络安全编程——Python编写基于UDP的主机发现工具(解码IP header)
python·网络协议·tcp/ip·安全·web安全·udp
北冥有羽Victoria1 小时前
OpenCLI 操作网页 从0到1完整实操指南
vscode·爬虫·python·github·api·ai编程·opencli
handsomestWei1 小时前
scikit-learn数据预处理模块
python·机器学习·scikit-learn
w_t_y_y1 小时前
机器学习常用的python包(二)工具箱scikit-learn
python·机器学习·scikit-learn
用户8356290780512 小时前
Python 自动拆分 Word 文档教程:按分节符与分页符处理
后端·python
陈天伟教授2 小时前
心电心音同步分析-案例:原型设计一
开发语言·人工智能·python·语言模型·架构