LeetCode:382. Linked List Random Node(蓄水池抽样算法C++)

382. Linked List Random 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?

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();


1、传说中的蓄水池抽样算法(此处水池大小为1),共有N个元素,等概推导:假定第m个选中的概率为1/m,则结果为1/m*(1-1/(m+1))…….(1-1/(N-1))(1-1/N)=1/m(m/(1+m))……((N-2)/(N-1))*((N-1)/N)=1/N。可见他的出现是所有数字中等概出现的。至于为什么不算前面的,因为我现在已经处理到了第m个,保存到了该值,只有后面的结果对他有影响。
2、随机数产生:

uniform_int_distribution<unsigned> u(0,i);
default_random_engine e(rand());
unsigned int m=u(e);

第一条语句是分布类型,闭区间[a,b];第二条语句是引擎;第三条映射(将分布类型映射到指定引擎)。注意到给引擎的种子,是为了不让序列重复。本来可以用time(0),但发现循环不管用,后来用rand()发现不会,百度得知他会在动调用srand()。另外,对于static,他可以让随机数接二连三产生,而不是重复的从一个地方产生。但在这里环境不能用,在vs里面尝试发现也不行,每次都会选中末尾元素,暂时还没有找出原因。
3、蓄水池抽样算法(水池大小为K):此时我们需要先给水池填满前K值,然后再由后续的值进行替换。思路都是一样的,推导由前面的1换成K就好了。


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    /** @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):cur(head) {
    }

    /** Returns a random node's value. */
    int getRandom() {
        int val=cur->val;
        ListNode *temp=cur;
        for(int i=0;temp!=nullptr;temp=temp->next,++i)
        {
            uniform_int_distribution u(0,i);
            default_random_engine e(rand());//真正随机的种子
            unsigned int m=u(e);
            if(m<1)
            {
                val=temp->val;
            }
        }
        return val;
    }
private:
    ListNode *cur;
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(head);
 * int param_1 = obj.getRandom();
 */

你可能感兴趣的:(leetcode,Algorithm)