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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 【LeetCode】382. Linked List Random Node 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组保存再随机选择 蓄水池抽样 日期 题目地址:ht ...

  4. LeetCode: Linked List Random Node

    这题参照http://blog.jobbole.com/42550/ 用的蓄水池算法,即更改ans的概率为1/(当前length) /** * Definition for singly-linked ...

  5. Leetcode 382. Linked List Random Node

    本题可以用reservoir sampling来解决不明list长度的情况下平均概率选择元素的问题. 假设在[x_1,...,x_n]只选一个元素,要求每个元素被选中的概率都是1/n,但是n未知. 其 ...

  6. [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 ...

  7. 382 Linked List Random Node 链表随机节点

    给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样.进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现?示例:// 初始化一个单链表 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. python+selenium安装指导

    一.      安装python 1.Window 平台安装 Python 以下为在 Window 平台上安装 Python 的简单步骤: 打开 WEB 浏览器访问https://www.python ...

  2. DWZ(J-UI)之路:错误

    1:关于左侧点击会把右边小窗口替换掉,导致右边永远只有一个小窗口. 方法:因为缺少了这个—— <li><a href="/admin/demo/index" ta ...

  3. 聊聊、Mybatis Java注解实现

    AbstractAnnotationConfigDispatcherServletInitializer public class MvcInitializer extends AbstractAnn ...

  4. 容器基础(二): 使用Namespace进行边界隔离

    Linux Namespace 容器技术可以认为是一种沙盒(sandbox), 为了实现沙盒/容器/应用间的隔离,就需要一种技术来对容器界定边界,从而让容器不至于互相干扰.当前使用的技术就是Names ...

  5. fragment中的WebView返回上一页

    public final class Text1Fm extends Fragment { static WebView mWeb; private View mContentView; privat ...

  6. maven中mapper.xml不发布的问题

    在自定义的包中定义了mapper.xml然后利用mybatis的扫描包形式来动态创建mapper 开启工程报错: 说无效的绑定 原因: 发布的war中,工程包中的mapper根本就没有出现在class ...

  7. 第十七篇:django基础(二)

    本篇内容 简单图书crm系统 编写views views:作为MVC中的C,接收用户的输入,调用数据库Model层和业务逻辑Model层,处理后将处理结果渲染到V层中去. app01/views.py ...

  8. Dubbo基础介绍

    基础知识 Dubbo是什么:Dubbo是一个分布式的服务框架,提供高性能和透明化的RPC远程调用方案,以及SOA服务治理方案 Dubbo涉及的知识: 远程调用:RMI.hassion.webservi ...

  9. 内存检测工具valgrind

    valgrind --tool=memcheck --leak-check=full --error-limit=no  --trace-children=yes  ./server valgrind ...

  10. 简单的FreeBSD 的内核编译

    简单的FreeBSD 的内核编译 删除并重新下载内核源码 删除自带的内核源码rm -rf /usr/src 下载内核源码wget https://download.freebsd.org/ftp/re ...