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. 【Hadoop离线基础总结】hive的窗口函数

    hive的窗口函数 概述 hive中一般求取TopN时就需要用到窗口函数 专业窗口函数一般有三个 rank() over dense rank() over row_number() over 实例 ...

  2. [hdu5200]离线+标记

    思路:按顺序处理,新建一堆然后向左右合并,不过巧妙地用了标记数组来记录和统计答案. #pragma comment(linker, "/STACK:10240000,10240000&quo ...

  3. [whu1564]后缀数组

    http://acm.whu.edu.cn/land/problem/detail?problem_id=1564 思路:先把串复制一遍,在末尾补个标记,后缀数组跑一下,扫一遍就ok了(过滤后缀在后半 ...

  4. web scraper插件爬虫进阶(能满足非技术人员的爬虫需求,建议收藏!!!!)

    为了照顾更多的小伙伴,大家的学习能力及了解程度都不同,因此大家可以通过以下目录来有选择性的学习,节约大家的时间. 备注:  一定要实操!!!            一定要实操!!!           ...

  5. 适用于任何Html内容的jQuery Slider插件 - AnySlider

    任何Slider都是一个易于使用且支持触摸的jQuery插件,允许您为任何html内容创建可自定义的滑块,如图像,文本,视频等. 特征: 重量轻,易于使用 支持键盘导航 使用淡入淡出或幻灯片过渡以及自 ...

  6. X-CTF(REVERSE高级) 666

    主函数输入的字符会和key比较长度和enflag比较内容,所以这道题的flag和输入有关 key长度为0x12,enflag的值为:izwhroz""w"v.K" ...

  7. 漫谈Huawei LiteOS五大内核模块

    [摘要]Huawei LiteOS是华为面向IoT领域,构建的“统一物联网操作系统和中间件软件平台”,以轻量级(内核小于10k).低功耗(1节5号电池最多可以工作5年),快速启动,互联互通,安全等关键 ...

  8. Unity3D的UGUI布局锚点自动绑定关系

    [MenuItem("CONTEXT/RectTransform/Auto")] public static void AutoRectAnior() { Debug.Log(&q ...

  9. flask之Flask-session三方组件

    from flask import Flask, views, render_template, request, session, redirect import redis as redis #p ...

  10. Django之ORM多表关系创建

    ORM模型多表逻辑创建: 以图书和作者关系模型为例: models.py from django.db import models ''' 一本书只能被一个出版社出版; 一个出版社可以出版多本书; 一 ...