问题:node
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?dom
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();
解决:this
① 先统计出链表的长度,而后根据长度随机生成一个位置,而后从开头遍历到这个位置便可。spa
class Solution { //142ms
ListNode head;
int len;
/** @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;
ListNode cur = head;
while(cur != null){
len ++;
cur = cur.next;
}
}
/** Returns a random node's value. */
public int getRandom() {
Random random = new Random();
int next = random.nextInt(len);
ListNode cur = head;
while(next != 0){
next --;
cur = cur.next;
}
return cur.val;
}
}.net
蓄水池抽样(Reservoir Sampling )是一个颇有趣的问题,它可以在o(n)时间内对n个数据进行等几率随机抽取,例如:从1000个数据中等几率随机抽取出100个。另外,若是数据集合的量特别大或者还在增加(至关于未知数据集合总量),该算法依然能够等几率抽样。对象
蓄水池抽样:从N个元素中随机的等几率的抽取k个元素,其中N没法肯定。ci
先给出代码:element
Init : a reservoir with the size: k for i= k+1 to N M=random(1, i); if( M < k) SWAP the Mth value and ith value end for上述伪代码的意思是:先选中第1到k个元素,做为被选中的元素。而后依次对第k+1至第N个元素作以下操做:
每一个元素都有k/x的几率被选中,而后等几率的(1/k)替换掉被选中的元素。其中x是元素的序号。
解法:咱们老是选择第一个对象,以1/2的几率选择第二个,以1/3的几率选择第三个,以此类推,以1/m的几率选择第m个对象。当该过程结束时,每个对象具备相同的选中几率,即1/n,证实以下。
证实:第m个对象最终被选中的几率P = 选择m的几率 * 其后面全部对象不被选择的几率,即
① 链表可能很长,咱们无法提早知道长度。因此使用蓄水池抽样来解决,具体步骤以下:
一、初始答案为第一个数,此时链表的下标指向第一个数,即此时第一个数被选中的几率为1;
二、下标后移一位指向第二个数,用Random函数随机抽取0-1的数,抽取的范围是2,抽中1的几率为1/2,若是抽中1,把答案改成此时下标所指的数,不然不改变答案的值。
三、以此类推,用Random函数抽取的范围不断加1,即Random rd = new Random(i),抽取范围为i,从0 -( i-1)中取到 i-1 的几率为1 / i。若是抽中i-1,把答案改成此时下标所指的数,不然不改变答案的值。
四、直到链表为空,获得答案;
第 i 个数被选中的几率为它被选中的几率:1 / i ,乘之后面的数不被选中的几率:[ i / ( i + 1 ) ] * [ ( i + 1 ) / ( i + 2 ) ] *... * [ ( n - 1 ) / ( n )]
即P(第 i 个数被选中) = ( 1 / i )* [ i / ( i + 1 ) ] * [ ( i + 1 ) / ( i + 2 ) ] *... * [ ( n - 1 ) / ( n)] = 1 / n 。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {//135ms
ListNode head;
Random random;
/** @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; random = new Random(); } /** Returns a random node's value. */ public int getRandom() { ListNode res = null; ListNode cur = head; for (int i = 1;cur != null;i ++){ if (random.nextInt(i) == 0){ res = cur; } cur = cur.next; } return res.val; } } /** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(head); * int param_1 = obj.getRandom(); */