当前位置:网站首页>leetcode-382.链表随机节点
leetcode-382.链表随机节点
2022-07-22 22:22:00 【KGundam】
数学问题-随机与取样
题目详情
给你一个单链表,随机选择链表的一个节点,并返回相应的节点值。每个节点 被选中的概率一样 。
实现 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中的一个,每个元素被返回的概率相等。
思路:
不同于数组,在未遍历完链表前,我们无法知道链表的总长度。这里我们就可以使用水库采
样:遍历一次链表,在遍历到第 m 个节点时,有 1/m 的概率选择这个节点覆盖掉之前的节点选择。
简单证明:
我的代码:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution
{
ListNode* h;
public:
Solution(ListNode* head) :h(head) {
} //将头节点用h存下来
int getRandom()
{
int ans = h->val; //初始化ans为头结点val
ListNode* node = h->next; //利用node往后遍历
int i = 2; //因为此时node是第2个节点,所以i初始化为2
while (node) // 遍历到末尾
{
//随机生成一个[0,i)的随机数,如果是0,
//就把答案改为该节点的数(1/i的概率)
if ((rand() % i) == 0)
ans = node->val;
//否则就继续往后遍历节点
++i;
node = node->next;
}
return ans;
}
};
/** * Your Solution object will be instantiated and called as such: * Solution* obj = new Solution(head); * int param_1 = obj->getRandom(); */
边栏推荐
- Experiment 2 YUV
- Wechat applet project practice
- 批量可视化目标检测标注框——YOLO格式数据集
- Spark troubleshooting -precondition eof: no length prefix available
- 传统银行票据打印系统几个关键技术点简要分析
- 张宇高数30讲总结
- How to use selenium.chrome to realize the extended function of intercepting or forwarding requests
- 组蛋白研究丨Worthington小牛胸腺组蛋白的特征及文献参考
- PNY file to picture
- 实验四 DPCM
猜你喜欢

matlab 分数阶pid控制

深入浅出地理解STM32中的中断系统——从原理到简单工程示例——保姆级教程

Celebrity interview | various strange current situations in the open source community -- night sky Book Chen Zili tison

Web resource sharing

老板要我做一个 IP 属地功能,一个开源库搞定!

postgresql数据库主从部署 主库挂了重新还原主库

云计算或成时代新拐点?从哪些点可以看出?

数的三次方根

RestClient操作索引库-初始化RestClient

MySQL消息队列表结构
随机推荐
Solution to the second game of 2022 Hangzhou Electric Multi school league
目标检测之锚点与锚框
数的三次方根
【读书笔记->统计学】12-01 置信区间的构建-置信区间概念简介
Qt+vtk+pcl pictures are converted to grayscale images and displayed with grayscale as the Y axis
Experiment 5 JPEG
Fledgling Xiao Li's 108th blog binary print
c语言扫雷
树以及二叉树的常用性质以及遍历
matlab simulink 磷酸铁锂电池仿真
matlab声音信号处理 频率图 信号过滤和播放声音
轮毂电机主动减振系统及其垂向性能优化
text-align:center居中
Detailed analysis of the 110th blog of the fledgling Xiao Li in stm32__ NVIC_ Setprioritygrouping (uint32_t prioritygroup) function
[reading notes > statistics] 12-01 construction of confidence interval - Introduction to the concept of confidence interval
Go 语言的 RSA 用秘钥解析JSEncrypt.js 这个库加密的密文失败
Google Earth Engine APP——一个完整的地图图例APP(美国西部土地利用分类)
开发者分享|MindSpore Lite 体验,一键实现图像分割
实验四 DPCM
matlab ode45求解微分方程