当前位置:网站首页>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()));
}
}
边栏推荐
- Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
- 2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)
- LeetCode刷题——摆动序列#376#Medium
- 2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
- Moment.js common methods
- 树状数组(单点修改区间查询和区间修改单点查询)
- 拉格朗日插值及其应用
- 强化学习科研知识必备(数据库、期刊、会议、牛人)
- 04-SDRAM:读操作(突发)
- Redux state management
猜你喜欢

Run the NPM will pop up to ask "how are you going to open this file?"

tidyverse笔记——dplyr包

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

Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software

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

postgresql源码学习(33)—— 事务日志⑨ - 从insert记录看日志写入整体流程

【微服务】(十六)—— 分布式事务Seata
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?

Leetcode952. 按公因数计算最大组件大小

(border-box) The difference between box model w3c and IE
随机推荐
2022.7.29 Array
【微服务】(十六)—— 分布式事务Seata
360 push-360 push tool-360 batch push tool
数据库概论 - MySQL的简单介绍
MySQL笔记下
SQL Server Datetime2数据类型
服务器和客户端信息的获取
【编程题】【Scratch三级】2022.03 冬天下雪了
MySQL系列一:账号管理与引擎
Install the gstreamer development dependency library to the project sysroot directory
Web浏览器工作流程解析
新瓶陈酒 --- 矩阵快速幂
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
从入门到一位合格的爬虫师,这几点很重要
Obtaining server and client information
Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
二叉树的还原(反序列化)
03-SDRAM: Write operation (burst)