当前位置:网站首页>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()));
}
}
边栏推荐
- 【 TA - frost Wolf _may - "one hundred plan" 】 art 2.3 hard surface
- 【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习
- [PSQL] 复杂查询
- leetcode 406. Queue Reconstruction by Height
- 【Go报错】go go.mod file not found in current directory or any parent directory 错误解决
- Install and use uView
- 二叉树的还原(反序列化)
- 基于LSTM的诗词生成
- tidyverse笔记——管道函数
- 文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
猜你喜欢

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

DirectExchange switch simple introduction demo

LeetCode:952. 按公因数计算最大组件大小【欧拉筛 + 并查集】

【网络攻防】常见的网络攻防技术——黑客攻防(通俗易懂版)

简单谈谈Feign

DirectExchange交换机简单入门demo

【科普向】5G核心网架构和关键技术

Some derivation formulas for machine learning backpropagation

Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model

Automatic translation software - batch batch automatic translation software recommendation
随机推荐
文件 - 03 下载文件:根据文件id获取下载链接
【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
事务的四大特性
【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
【 TA - frost Wolf _may - "one hundred plan" 】 art 2.3 hard surface
从 Google 离职,前Go 语言负责人跳槽小公司
双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分
Detailed explanation of js prototype
How to choose a suitable UI component library in uni-app
线程中断方法
【云原生】-Docker安装部署分布式数据库 OceanBase
拉格朗日插值及其应用
Gradle剔除依赖演示
自动翻译软件-批量批量自动翻译软件推荐
Zabbix6.2惊喜发布!特别优化中大型环境部署的性能!
opencv、pil和from torchvision.transforms的Resize, Compose, ToTensor, Normalize等差别
基于LSTM的诗词生成
codec2 BlockPool:unreadable libraries
gstreamer's caps event and new_segment event
【第四章】详解Feign的实现原理