摘要:题目要求要求从单链表中,随机返回一个节点的值,要求每个节点被选中的概率是相等的。假如一共有个物品,需要从其中挑选出个物品,要求确保个物品中每个物品都能够被等概率选中。对于这种等概率问题,简答的做法是通过随机数获取选中物品的下标。
题目要求
</>复制代码
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(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
Solution solution = new Solution(head);
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
solution.getRandom();
要求从单链表中,随机返回一个节点的值,要求每个节点被选中的概率是相等的。
思路和代码在等概率随机选择算法中,最经典的算法就是蓄水池算法。可以参考同类型题目398 random pick index。这里再次整理一下蓄水池算法的思路和简单证明。
假如一共有N个物品,需要从其中挑选出K个物品,要求确保N个物品中每个物品都能够被等概率选中。对于这种等概率问题,简答的做法是通过随机数获取选中物品的下标。但是蓄水池算法允许我们从数据流的角度来随机获得K个物品,即在并不知道总体的样本数有多少的情况下,随机抽取K个物品。
蓄水池算法的思路如下:
选中前K个物品放入蓄水池
对于第K+1个物品,其被选中并替换蓄水池中任意一个物品的概率为K/(K+1)
对于第K+i个物品,其被选中并替换蓄水池中任意一个物品的概率为K/(K+i)
重复这个步骤直到K+i=N
对于这个算法,我们可以采用归纳法进行简单证明。已知对于前K个物品,每个物品的被选中的概率为1,满足了K/K=1的概率。
对于K+i-1个物品,假设每个物品被选中的概率为K/(K+i-1)。证明对于前K+i个物品,每个物品被放入蓄水池中的概率为K/(K+i)
对于第K+i个物品,其被选中并替换蓄水池中任意一个物品的概率为K/(K+i)
对于之前在蓄水池中的物品,其仍在蓄水池中的概率为之前被选中在蓄水池中概率乘以这一次未被换出蓄水池的概率,即P = P(上一轮在蓄水池中) * P(这一轮没有被替换掉)。对此进行计算,P(上一轮在蓄水池中) * P(这一轮没有被替换掉) = P(上一轮在蓄水池中) * (1-P(这一轮被替换掉)) = (K / (K+i-1)) * (1 - (P * 1/K)),算出P = K/(K+i)
证明对于前K+i个物品,每个物品被放入蓄水池中的概率为K/(K+i),当K+i等于N时,每个物品被选中的概率为K/N
在本题中,使用蓄水池算法的N为单链表的长度,K为1。
代码如下:
</>复制代码
private ListNode head;
private Random r;
/** @param head The linked list"s head.
Note that the head is guaranteed to be not null, so it contains at least one node. */
public Solution(ListNode head) {
this.head = head;
this.r = new Random();
}
/** Returns a random node"s value. */
public int getRandom() {
ListNode tmp = this.head;
int result = 0;
int index = 1;
do{
if(r.nextInt(index) == 0) {
result = tmp.val;
}
tmp = tmp.next;
index++;
}while(tmp != null);
return result;
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/74862.html
Problem Given a singly linked list, return a random nodes 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...
LeetCode[138] Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of t...
摘要:大体意思就是,先复制到,顺便将所有的放在再复制所有的到,顺便将所有的放在最后令,令,将和分离,返回的头结点 Problem A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. ...
摘要:栈迭代复杂度时间空间如果不算新链表的空间则是思路由于随机指针有可能产生环路,我们不能直接沿着随机指针的方向一个一个复制。同时我们又不能沿着指针直接复制,因为我们不知道随机指针所指向的新节点是哪个。 Copy List with Random Pointer A linked list is given such that each node contains an additiona...
摘要:给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。要求返回这个链表的深拷贝。提示你必须返回给定头的拷贝作为对克隆列表的引用。确定随机节点的关系之后再拆分链表。其时间复杂度为,空间复杂度为。 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。 要求返回这个链表的深拷贝。 A linked list is g...
阅读 3218·2021-08-03 14:05
阅读 2229·2019-08-29 15:35
阅读 749·2019-08-29 13:30
阅读 3257·2019-08-29 13:20
阅读 2608·2019-08-23 18:15
阅读 1878·2019-08-23 14:57
阅读 2299·2019-08-23 13:57
阅读 1421·2019-08-23 12:10