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
相关推荐
酷爱码2 分钟前
python如何遍历postgresql所有的用户表
python·postgresql·oracle
freyazzr9 分钟前
Leetcode刷题 | Day60_图论06
数据结构·c++·算法·leetcode·图论
广药门徒13 分钟前
OpenMV IDE 的图像接收缓冲区原理
前端·人工智能·python
搂……住13 分钟前
第二道re
python
qq_5845989218 分钟前
day30python打卡
开发语言·人工智能·python·算法·机器学习
火兮明兮29 分钟前
Python训练第三十天
开发语言·python
freyazzr42 分钟前
Leetcode刷题 | Day64_图论09_dijkstra算法
数据结构·c++·算法·leetcode·图论
珊瑚里的鱼1 小时前
【滑动窗口】LeetCode 1004题解 | 最大连续1的个数 Ⅲ
开发语言·c++·笔记·算法·leetcode
L_cl1 小时前
【Python 算法零基础 4.排序 ② 冒泡排序】
数据结构·python·算法
小峰编程1 小时前
Python函数——万字详解
linux·运维·服务器·开发语言·前端·网络·python