470. 用 Rand7() 实现 Rand10()

已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数。

不要使用系统的 Math.random() 方法。

示例 1:

输入: 1

输出: [7]

示例 2:

输入: 2

输出: [8,4]

示例 3:

输入: 3

输出: [8,1,10]

提示:

rand7 已定义。

传入参数: n 表示 rand10 的调用次数。

进阶:

rand7()调用次数的 期望值 是多少 ?

你能否尽量少调用 rand7() ?

/**
* 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;
}
}

Java实现 LeetCode 470 用 Rand7() 实现 Rand10()的更多相关文章

  1. LeetCode 470. 用 Rand7() 实现 Rand10()(Implement Rand10() Using Rand7())

    题目描述 已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数. 不要使用系统的 Math.random() 方法. 示 ...

  2. leetcode 470. 用 Rand7() 实现 Rand10() (数学,优化策略)

    题目链接 https://leetcode-cn.com/problems/implement-rand10-using-rand7/ 题意: 给定一个rand7()的生成器,求解如何产生一个rand ...

  3. 470. 用 Rand7() 实现 Rand10()

    已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数. public class Solution { public s ...

  4. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  8. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. Linux内核驱动学习(十)Input子系统详解

    文章目录 前言 框架 如何实现`input device` 设备驱动? 头文件 注册input_dev设备 上报按键值 dev->open()和dev->close() 其他事件类型,处理 ...

  2. Android 源码结构分析

    源码版本:AOSP_7.1.1 硬件平台:Rockchip 由于工作要求,需要对rockchip平台的安卓系统进行剪裁.安卓源码比较庞大,会让人感到无从下手,对此,有必要了解一下源码的大致目录结构以及 ...

  3. HDU 2001 (水)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2001 题目大意:两个点求距离 解题思路: 套基本公式 a = √(b2 + c2); 小数点后几位的表 ...

  4. at命令用法详解

    在linux系统中你可能已经发现了为什么系统常常会自动的进行一些任务?这些任务到底是谁在支配他们工作的? 在linux系统如果你想要让自己设计的备份程序可以自动在某个时间点开始在系统底下运行,而不需要 ...

  5. webpack从零的实践(新手良药)

    1. 什么是webpack? 本质上,webpack是一个现代javascript应用程序的静态模块打包器.webpack处理应用程序时,它会递归地构建一个依赖关系图(dependency graph ...

  6. yield与park的区别

    yield表示放弃本次cpu的时间片,但是操作系统在下一个时间片依旧可能会调用该线程/进程 park表示线程/进程睡眠,需要让其他线程/进程唤醒,才有可能重新被操作系统分配时间片, 非自旋锁,底层一般 ...

  7. express.static设置缓存

    之前因为服务器端脚本不大,都是直接手写,按请求文件后缀名设置cache-control的max-age. 今天决定还是改成express,发现原来express.static()方法设置缓存,直接在参 ...

  8. Python--WebDriverWait+expected_conditions的一个应用

    from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.s ...

  9. python中的字典dict

    字典的常用操作及方法 增: dic[key]=value 有则修改,无则添加 dic.setdefault( ) 有则不变,无则添加:有键无值则值为None, 删:  dic.pop(key) 删除后 ...

  10. 容器技术之Docker基础入门

    前文我们了解了下LXC的基础用法以及图形管理工具LXC WEB Panel的简单使用,有兴趣的朋友可以参考https://www.cnblogs.com/qiuhom-1874/p/12904188. ...