2-211-(LeetCode-470) 用 Rand7() 实现 Rand10()

发布时间 2023-04-17 18:04:45作者: 白露~

 

1. 题目

 

https://leetcode.cn/problems/implement-rand10-using-rand7/submissions/425373186/

 

2. 解法

 

class Solution extends SolBase {
    public int rand10() {
        int temp = 40;
        while (temp >= 40) {
            temp = (rand7() - 1) * 7 + rand7()-1;
        }

        return temp % 10 + 1;
    }
}

  

3. 总结