470. 用 Rand7() 实现 Rand10()
给定方法 rand7
可生成 [1,7]
范围内的均匀随机整数,试写一个方法 rand10
生成 [1,10]
范围内的均匀随机整数。
你只能调用 rand7()
且不能调用其他方法。请不要使用系统的 Math.random()
方法。
每个测试用例将有一个内部参数 n
,即你实现的函数 rand10()
在测试时将被调用的次数。请注意,这不是传递给 rand10()
的参数。
示例 1:
输入: 1 输出: [2]
示例 2:
输入: 2 输出: [2,8]
示例 3:
输入: 3 输出: [3,8,10]
提示:
1 <= n <= 105
进阶:
rand7()
调用次数的 期望值 是多少 ?rand7()
?原站题解
python3 解法, 执行用时: 276 ms, 内存消耗: 17.7 MB, 提交时间: 2022-12-08 12:01:29
# The rand7() API is already defined for you. # def rand7(): # @return a random integer in the range 1 to 7 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
golang 解法, 执行用时: 8 ms, 内存消耗: 5.5 MB, 提交时间: 2022-12-08 12:01:07
func rand10() int { for { 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 } } }
javascript 解法, 执行用时: 88 ms, 内存消耗: 48.4 MB, 提交时间: 2022-12-08 12:00:54
/** * The rand7() API is already defined for you. * var rand7 = function() {} * @return {number} a random integer in the range 1 to 7 */ var rand10 = function() { var a, b, idx; while (true) { a = rand7(); b = rand7(); idx = b + (a - 1) * 7; if (idx <= 40) { return 1 + (idx - 1) % 10; } a = idx - 40; b = rand7(); // get uniform dist from 1 - 63 idx = b + (a - 1) * 7; if (idx <= 60) { return 1 + (idx - 1) % 10; } a = idx - 60; b = rand7(); // get uniform dist from 1 - 21 idx = b + (a - 1) * 7; if (idx <= 20) { return 1 + (idx - 1) % 10; } } };
java 解法, 执行用时: 5 ms, 内存消耗: 47.3 MB, 提交时间: 2022-12-08 12:00:37
/** * The rand7() API is already defined in the parent class SolBase. * public int rand7(); * @return a random integer in the range 1 to 7 */ class Solution extends SolBase { public int rand10() { int a, b, idx; while (true) { a = rand7(); b = rand7(); idx = b + (a - 1) * 7; if (idx <= 40) { return 1 + (idx - 1) % 10; } a = idx - 40; b = rand7(); // get uniform dist from 1 - 63 idx = b + (a - 1) * 7; if (idx <= 60) { return 1 + (idx - 1) % 10; } a = idx - 60; b = rand7(); // get uniform dist from 1 - 21 idx = b + (a - 1) * 7; if (idx <= 20) { return 1 + (idx - 1) % 10; } } } }
java 解法, 执行用时: 5 ms, 内存消耗: 46.8 MB, 提交时间: 2022-12-08 11:59:27
/** * The rand7() API is already defined in the parent class SolBase. * public int rand7(); * @return a random integer in the range 1 to 7 */ class Solution extends SolBase { public int rand10() { int row, col, idx; do { row = rand7(); col = rand7(); idx = col + (row - 1) * 7; } while (idx > 40); return 1 + (idx - 1) % 10; } }
javascript 解法, 执行用时: 72 ms, 内存消耗: 48.7 MB, 提交时间: 2022-12-08 11:59:13
/** * The rand7() API is already defined for you. * var rand7 = function() {} * @return {number} a random integer in the range 1 to 7 */ var rand10 = function() { var row, col, idx; do { row = rand7(); col = rand7(); idx = col + (row - 1) * 7; } while (idx > 40); return 1 + (idx - 1) % 10; };
golang 解法, 执行用时: 12 ms, 内存消耗: 5.5 MB, 提交时间: 2022-12-08 11:57:52
func rand10() int { for { row := rand7() col := rand7() idx := (row-1)*7 + col if idx <= 40 { return 1 + (idx-1)%10 } } }
python3 解法, 执行用时: 316 ms, 内存消耗: 17.5 MB, 提交时间: 2022-12-08 11:57:35
# The rand7() API is already defined for you. # def rand7(): # @return a random integer in the range 1 to 7 class Solution: def rand10(self) -> int: while True: row = rand7() col = rand7() idx = (row - 1) * 7 + col if idx <= 40: return 1 + (idx - 1) % 10