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
相关推荐
河码匠1 分钟前
Django rest framework 自定义url
后端·python·django
cnxy1882 分钟前
Python Web开发新时代:FastAPI vs Django性能对比
前端·python·fastapi
weixin_462446238 分钟前
【原创实践】Windows 和 Linux 下使用 Python 3.10 搭建 PaddleOCRVL 识别图片并100%还原表格
linux·windows·python·飞浆
ID_180079054738 分钟前
除了Python,还有哪些语言可以解析淘宝商品详情API返回的JSON数据?
开发语言·python·json
Irene.ll1 小时前
DAY23
python
专注于大数据技术栈1 小时前
java学习--Collection的迭代器
java·python·学习
梨落秋霜9 小时前
Python入门篇【文件处理】
android·java·python
Java 码农9 小时前
RabbitMQ集群部署方案及配置指南03
java·python·rabbitmq
张登杰踩10 小时前
VIA标注格式转Labelme标注格式
python
Learner10 小时前
Python数据类型(四):字典
python