当前位置:网站首页>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()));
}
}
边栏推荐
- 2022.07.13_每日一题
- 【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习
- Conditional statements of shell (test, if, case)
- 完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
- 解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
- 2022.07.18_每日一题
- 2022.07.12_每日一题
- Kubernetes scheduling
- ‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
- opencv、pil和from torchvision.transforms的Resize, Compose, ToTensor, Normalize等差别
猜你喜欢

双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分

服务器和客户端信息的获取

从入门到一位合格的爬虫师,这几点很重要

360推送-360推送工具-360批量推送工具

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

【解决】npm ERR A complete log of this run can be found in npm ERR

那些破釜沉舟入局Web3.0的互联网精英都怎么样了?

2022.07.14_每日一题

DAY18:Xss 靶场通关手册

简单谈谈Feign
随机推荐
项目 - 如何根据最近30天、最近14天、最近7天、最近24小时、自定义时间范围查询MySQL中的数据?
2022.07.22_每日一题
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
2022.7.29 Array
Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition
van-uploader上传图片,使用base64回显无法预览的问题
把 VS Code 当游戏机
leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
关于求反三角函数的三角函数值
Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
R——避免使用 col=0
【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
《白帽子说Web安全》思维导图
Install the gstreamer development dependency library to the project sysroot directory
强化学习科研知识必备(数据库、期刊、会议、牛人)
【并发编程】ReentrantLock的lock()方法源码分析
剑指offer(一)
【第四章】详解Feign的实现原理
‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record