Reservoir Sampling-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();
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=;temp!=nullptr;temp=temp->next,++i)
{
uniform_int_distribution<unsigned> u(,i);
default_random_engine e(rand());//真正随机的种子
unsigned int m=u(e);
if(m<)
{
val=temp->val;
}
}
return val;
}
private:
ListNode *cur;
};
Reservoir Sampling-382. Linked List Random Node的更多相关文章
- [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
本题可以用reservoir sampling来解决不明list长度的情况下平均概率选择元素的问题. 假设在[x_1,...,x_n]只选一个元素,要求每个元素被选中的概率都是1/n,但是n未知. 其 ...
- 【LeetCode】382. Linked List Random Node 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组保存再随机选择 蓄水池抽样 日期 题目地址:ht ...
- 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 ☆☆☆
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(蓄水池采样)
1. 问题 给定一个单链表,随机返回一个结点,要求每个结点被选中的概率相等. 2. 思路 在一个给定长度的数组中等概率抽取一个数,可以简单用随机函数random.randint(0, n-1)得到索引 ...
- 382 Linked List Random Node 链表随机节点
给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样.进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现?示例:// 初始化一个单链表 ...
- [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: Linked List Random Node
这题参照http://blog.jobbole.com/42550/ 用的蓄水池算法,即更改ans的概率为1/(当前length) /** * Definition for singly-linked ...
随机推荐
- WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [] milliseconds.
编译安装tomcat-native和tomcat-deamon以后,发现toomcat启动很慢,好久才有响应.以下日志供参考: 11-Sep-2017 12:19:28.102 INFO [main] ...
- aspx导出文件
System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw ...
- c++11多线程学习笔记之二 mutex使用
// 1111111.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include ...
- 2018.07.30 bzoj4355: Play with sequence(线段树)
传送门 维护区间覆盖成非负数,区间变成max(xi+a,0)" role="presentation" style="position: relative;&q ...
- 2018.07.25 bzoj2125: 最短路(圆方树+倍增)
传送门 人生的第一道仙人掌. 这道题求是仙人掌上的最短路. 先建出圆方树,然后用倍增跑最短路,当lca" role="presentation" style=" ...
- hdu-1058(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 题意:求只由2,3,5,7的乘积组成的数,输出格式见output 思路:开始想打表,后来打表超时 ...
- Linux IPC之共享内存
System V共享内存机制: shmget shmat shmdt shmctl 原理及实现: system V IPC机制下的共享内存本质是一段特殊的内存区域,进程间需要共享的数据被放在该共 ...
- TopK排序
利用快速排序实现TopK排序 //返回支点的下标 int partition(int *arr, int low, int high) { //选取第一个元素为支点 int pivot = arr[l ...
- Proxy Hosted Virtual
http://books.sonatype.com/nexus-book/reference/confignx-sect-manage-repo.html Public Repositories 对外 ...
- 17)maven-surefire-plugin
http://maven.apache.org/surefire/maven-surefire-plugin/ Goals Overview The Surefire Plugin has only ...