当前位置: 首页 > news >正文

Java | Leetcode Java题解之第470题用Rand7()实现Rand10()

题目:

题解:

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 - 63idx = b + (a - 1) * 7;if (idx <= 60) {return 1 + (idx - 1) % 10;}a = idx - 60;b = rand7();// get uniform dist from 1 - 21idx = b + (a - 1) * 7;if (idx <= 20) {return 1 + (idx - 1) % 10;}}}
}

http://www.mrgr.cn/news/47588.html

相关文章:

  • 使用 Raspberry Pi Pico W 的基于 MQTT 的分布式网络自适应估计
  • Java | Leetcode Java题解之第472题连接词
  • 【CSS in Depth 2 精译_047】7.2 CSS 响应式设计中的媒体查询原则(上):深入理解媒体查询的类型
  • QTableView-mode中嵌入复选框CheckBox
  • 编程思想:编程范式:响应式编程
  • 快速解决urllib3.exceptions.MaxRetryError: HTTPSConnectionPool
  • Unity网络开发 - C#开源网络通信库PESocket的使用
  • 7.C++面向对象3(拷贝构造函数,赋值运算符重载)
  • Renesas R7FA8D1BH (Cortex®-M85) 上超声波测距模块(HC-SR04)驱动开发
  • Effective C++笔记之二十四:stack overflow
  • window.location.href和open的区别
  • QD1-P14 HTML常用标签:input输入标签
  • MySQL--事务(详解)
  • PGMP-00基础单词(1-25)
  • 数学基础 -- 三角函数极限之小数场景
  • 【.NET 8 实战--孢子记账--从单体到微服务】--角色(增加/删除/修改/查询)
  • React技术在Meta Connect 2024大会
  • LeetCode15.三数之和
  • 【cpp】 lambda 表达式常用笔记
  • ViT模型技术学习