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. neo4j在docker容器环境中无法启动的问题

    回去过了个周末,neo4j就无法启动了 数据还没备份出来,着急啊.上周回去前刚刚在研究怎么把数据导出来,尝试了一些容器导出的方法,没有成功.周一回来就无法启动了... 表现为启动后过几十秒又变为sto ...

  2. python第七天--文件练

    目的: 将不同人物说的话分别保存下来 以==========分段 代码: f=open('record.txt','r',encoding='UTF-8') zyf=[] smy=[] count=1 ...

  3. hive经典练习题

    一.建表和加载数据 1.student表 create table if not exists student(s_id int,s_name string,s_birth string,s_sex ...

  4. Mysql 常用语句实战(3)

    前置 sql 语句 用来创建表.插入数据 ; DROP TABLE IF EXISTS dept_;-- 部门表 DROP TABLE IF EXISTS emp_;-- 部门表 ; SELECT @ ...

  5. layui select下拉菜单联动

    做的比较简单,先从后台直接把第一级菜单输出,然后点击二级菜单的时候再动态展示 <div class="layui-inline"> <label class=&q ...

  6. Redis系列(七)Redis面试题

    Redis 系列: Redis系列(一)Redis入门 Redis系列(二)Redis的8种数据类型 Redis系列(三)Redis的事务和Spring Boot整合 Redis系列(四)Redis配 ...

  7. 黑马程序员_毕向东_Java基础视频教程——转义字符(随笔)

    转义字符 转义字符 通过 \ 来转变后面的字母或符号的含义 \n :换行 \b :退格.相当于 backspace \r : 相当于回车键. Windows系统中,回车是由两个字符来表示 \r \n. ...

  8. JS的函数和对象二

    复习 递归,在函数内部调用自身  return 匿名函数  function(){   } 创建函数,函数表达式  var fn=function(){   } 自调用   (function(){ ...

  9. Aangular 父子间组件传递

    1.父子间组件传递------重点&难点 Vue.js和Angular中的父子间消息传递原理一样,都可以用口诀: “Props Down,Events Up” 方向1:父 =>子 父组件 ...

  10. 关于hadoop3.x MR报错:找不到或无法加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMaster

    用的apache Hadoop3.X,今天运行MR报错: 找不到或无法加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMaster 关键需要配置两个配置:ma ...