当前位置:网站首页>2022.07.13_每日一题
2022.07.13_每日一题
2022-07-31 06:07:00 【诺.い】
382. 链表随机节点
题目描述
给你一个单链表,随机选择链表的一个节点,并返回相应的节点值。每个节点 被选中的概率一样 。
实现 Solution 类:
Solution(ListNode head)使用整数数组初始化对象。int getRandom()从链表中随机选择一个节点并返回该节点的值。链表中所有节点被选中的概率相等。
示例:

输入
[“Solution”, “getRandom”, “getRandom”, “getRandom”, “getRandom”, “getRandom”]
[[[1, 2, 3]], [], [], [], [], []]
输出
[null, 1, 3, 2, 2, 3]
解释
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // 返回 1
solution.getRandom(); // 返回 3
solution.getRandom(); // 返回 2
solution.getRandom(); // 返回 2
solution.getRandom(); // 返回 3
// getRandom() 方法应随机返回 1、2、3中的一个,每个元素被返回的概率相等。
提示:
- 链表中的节点数在范围
[1, 104]内 -104 <= Node.val <= 104- 至多调用
getRandom方法104次
进阶:
- 如果链表非常大且长度未知,该怎么处理?
- 你能否在不使用额外空间的情况下解决此问题?
- 水塘抽样
- 链表
- 数学
- 随机化
coding
class Solution1 {
ListNode head;
int size;
public Solution(ListNode head) {
this.head = head;
ListNode cur = head;
while (cur != null) {
cur = cur.next;
size ++;
}
}
public int getRandom() {
int randomNum = (int) (Math.random() * size);
int cnt = 0;
ListNode cur = head;
while (cur != null) {
if (cnt == randomNum) {
return cur.val;
}
cur = cur.next;
cnt ++;
}
return 0;
}
}
// ---------------------------------------------------------------
class Solution2 {
List<Integer> list;
public Solution(ListNode head) {
list = new ArrayList<>();
while (head != null) {
list.add(head.val);
head = head.next;
}
}
public int getRandom() {
return list.get((int) (Math.random() * list.size()));
}
}
边栏推荐
- 我开发了一个利用 Bun 执行 .ts / .js 文件的 VS Code 插件
- leetcode 406. Queue Reconstruction by Height
- 简单谈谈Feign
- What is float?What is document flow?Several ways and principles of clearing floats?What is BFC, how to trigger BFC, the role of BFC
- 线程中断方法
- Automatic translation software - batch batch automatic translation software recommendation
- 文件 - 04 下载文件: 根据文件下载链接下载文件
- 自动翻译软件-批量批量自动翻译软件推荐
- Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
- Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
猜你喜欢

R——避免使用 col=0

科普 | “大姨太”ETH 和 “小姨太”ETC的爱恨情仇

Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning

Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题

基于LSTM的诗词生成

【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念

芯塔电子斩获第十一届中国双创大赛芜湖赛区桂冠

毫米波技术基础

MySql的安装配置超详细教程与简单的建库建表方法

解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
随机推荐
【云原生】-Docker容器迁移Oracle到MySQL
PCB抄板
2022.7.29 Array
高并发与多线程之间的难点对比(容易混淆)
CHI论文阅读(1)EmoGlass: an End-to-End AI-Enabled Wearable Platform for Enhancing Self-Awareness of Emoti
Analysis of pseudo-classes and pseudo-elements
codec2 BlockPool:不可读库
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
postgresql源码学习(33)—— 事务日志⑨ - 从insert记录看日志写入整体流程
shell之条件语句(test、if、case)
第十七章:回溯探求指定入口的马步遍历,贪心无回溯探求马步遍历,递归探求nxm棋盘带障碍马步遍历
Detailed explanation of js prototype
Kubernetes scheduling
How to choose a suitable UI component library in uni-app
03-SDRAM: Write operation (burst)
【第四章】详解Feign的实现原理
Basic usage of Koa framework
HuffmanTree
什么是半波整流器?半波整流器的使用方法
关于求反三角函数的三角函数值