当前位置:网站首页>2022.07.13_Daily Question
2022.07.13_Daily Question
2022-07-31 07:40:00 【No. い】
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()));
}
}
边栏推荐
- PCB抄板
- 解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
- 第十六章:构建n(5,7)阶素数幻方
- 【解决】npm ERR A complete log of this run can be found in npm ERR
- 03-SDRAM:写操作(突发)
- opencv、pil和from torchvision.transforms的Resize, Compose, ToTensor, Normalize等差别
- 04-SDRAM:读操作(突发)
- 小实战项目之——吃货联盟订餐系统
- 2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
- Some derivation formulas for machine learning backpropagation
猜你喜欢

DAY18: Xss Range Clearance Manual

postgresql源码学习(34)—— 事务日志⑩ - 全页写机制

【第四章】详解Feign的实现原理

剑指offer(一)

Obtaining server and client information

2022.07.20_每日一题

【微服务】(十六)—— 分布式事务Seata

嵌入式系统驱动初级【2】——内核模块下_参数和依赖

Web浏览器工作流程解析

Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
随机推荐
tidyverse笔记——管道函数
Zabbix6.2惊喜发布!特别优化中大型环境部署的性能!
2022.07.22_每日一题
04-SDRAM: Read Operation (Burst)
2022.07.18_每日一题
【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
第十六章:构建n(5,7)阶素数幻方
双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分
2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)
Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
第十七章:回溯探求指定入口的马步遍历,贪心无回溯探求马步遍历,递归探求nxm棋盘带障碍马步遍历
04-SDRAM:读操作(突发)
2022.07.15_每日一题
bcos简介及自序
【解决】npm ERR A complete log of this run can be found in npm ERR
DirectExchange switch simple introduction demo
Detailed explanation of js prototype
基于LSTM的诗词生成
剑指offer(一)
2022.07.14_每日一题