Linked List Random Node -- LeetCode
Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen.
Follow up:
What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space?
Example:
// Init a singly linked list [1,2,3].
ListNode head = new ListNode();
head.next = new ListNode();
head.next.next = new ListNode();
Solution solution = new Solution(head); // getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
solution.getRandom();
思路:从n个数中选k个数,每个数被选中的几率是k/n的方法:
将n个数的前k个数选出来,构成一个备选集合。然后从第k+1个数开始向后遍历直到第n个数。遍历第k+1个数时,我们用k/(k+1)的几率选中它,然后随机替换掉备选集合中的一个数。则此时对于备选集合中的每个数,不被替换掉的几率是 1 - P(第k+1个数被选中并替换掉该数) = 1 - k/(k+1) * 1/k = k/(k+1)。
假设遍历到第i个数时,一个数留在备选集合中的概率是k/i。则对于第i+1个数,我们用k/(i+1)的概率选中它,并随机替换掉备选集合中的一个数,则对于备选集合中的每一个数,仍然留在集合中的概率是P(该数在上一次流了下来)(1-P(第i+1个数被选中并替换掉该数))= k/i * (1 - k/(i+1) * 1/k) = k/(i+1)。
因此,当我们进行到第n个数时,该数进入备选集合的概率是k/n, 备选集合中其他的数留下的概率是k/n。最后备选集合中的数就是我们要随机选出的k个数,每个数被选出的几率是k/n。
这个题里,让k等于1即可。因此备选集合中始终只有一个值,遍历第i个数时,用1/i的概率选中它并与备选值进行替换。最后的备选值就是随机选出的数,选取的概率为1/n。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* head;
/** @param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node. */
Solution(ListNode* head) {
this->head = head;
} /** Returns a random node's value. */
int getRandom() {
ListNode* res;
int count = ;
for (ListNode* i = head; i != NULL; i = i->next, count++) {
srand(time(NULL));
int roll = rand() % count + ;
if (roll == ) res = i;
}
return res->val;
}
}; /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/
Linked List Random Node -- LeetCode的更多相关文章
- [LeetCode] Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [LeetCode] 382. Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- 【LeetCode】382. Linked List Random Node 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组保存再随机选择 蓄水池抽样 日期 题目地址:ht ...
- LeetCode: Linked List Random Node
这题参照http://blog.jobbole.com/42550/ 用的蓄水池算法,即更改ans的概率为1/(当前length) /** * Definition for singly-linked ...
- Leetcode 382. Linked List Random Node
本题可以用reservoir sampling来解决不明list长度的情况下平均概率选择元素的问题. 假设在[x_1,...,x_n]只选一个元素,要求每个元素被选中的概率都是1/n,但是n未知. 其 ...
- [LeetCode] 382. Linked List Random Node ☆☆☆
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- 382 Linked List Random Node 链表随机节点
给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样.进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现?示例:// 初始化一个单链表 ...
- 382. Linked List Random Node
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [Swift]LeetCode382. 链表随机节点 | Linked List Random Node
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
随机推荐
- apt-get阿里源
备份原有配置文件 mv /etc/apt/sources.list /etc/apt/sources.list.bak 新建一个文件 vi /etc/apt/sources.list 复制以下内容到新 ...
- PoolManager
我用的PoolManager版本是5.5.2的,导入的包总共有三个文件夹:Editor,Plugins,PoolManagerExampleFiles 1.Editor这个文件夹里面的东西,顾名思义, ...
- Python全栈工程师(元组、字典)
ParisGabriel 感谢 大家的支持 你们的阅读评价就是我最好的更新动力 我会坚持吧排版做的越来越好 每天坚持 一天一篇 点个订阅吧 灰常感谢 当个死粉也阔以 ...
- PAT——乙级1028
这道题花了我半个多小时,对呀乙级算是挺多时间的了. 1028 人口普查 (20 point(s)) 某城镇进行人口普查,得到了全体居民的生日.现请你写个程序,找出镇上最年长和最年轻的人. 这里确保每个 ...
- 深入探讨ui框架
深入探讨前端UI框架 1 前言 先说说这篇文章的由来 最近看riot的源码,发现它很像angular的dirty check,每个component ( tag )都保存一个expressions数组 ...
- Redis Sorted Set
Redis Sorted Set Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是通过分数来为集合 ...
- Arcgis桌面开发,Python引用GDAL库的方法
我用的是arcgis10.2,python版本是arcgis自动安装的Pythin2.7 1.下载gdal-111-1700-core.msi和对应的GDAL-1.11.1.win32-py2.7.m ...
- [洛谷P3810]【模板】三维偏序(陌上花开)
题目大意:有$n$个元素,第$i$个元素有三个属性$a_i,b_i,c_i$,设$f(i)=\sum\limits_{i\not = j}[a_j\leqslant a_i,b_j\leqslant ...
- 数表( table )
数表( table ) 题目描述 有一张n×m的数表,其第i行第j列(1≤i≤n,1≤j≤m)的数值为能同时整除i和j的所有自然数之和.给定a,计算数表中不大于a的数之和. 输入 输入包含多组数据. ...
- FOJ ——Problem 1759 Super A^B mod C
Problem 1759 Super A^B mod C Accept: 1368 Submit: 4639Time Limit: 1000 mSec Memory Limit : 32 ...