给定一个单链表,随机选择链表的一个节点,并返回相应的节点值。保证每个节点被选的概率一样。
进阶:
如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现?
示例:
// 初始化一个单链表 [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()方法应随机返回1,2,3中的一个,保证每个元素被返回的概率相等。
solution.getRandom();
详见:https://leetcode.com/problems/linked-list-random-node/description/

C++:

方法一:

/**
* 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) {
len=0;
this->head=head;
ListNode *cur=head;
while(cur)
{
++len;
cur=cur->next;
}
} /** Returns a random node's value. */
int getRandom() {
int t=rand()%len;
ListNode *cur=head;
while(t)
{
--t;
cur=cur->next;
}
return cur->val;
}
private:
int len;
ListNode *head;
}; /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/

方法二:

/**
* 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) {
this->head=head;
} /** Returns a random node's value. */
int getRandom() {
int res=head->val;
int i=2;
ListNode *cur=head->next;
while(cur)
{
int j=rand()%i;
if(j==0)
{
res=cur->val;
}
++i;
cur=cur->next;
}
return res;
}
private:
ListNode *head;
}; /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/

参考:https://www.cnblogs.com/grandyang/p/5759926.html

382 Linked List Random Node 链表随机节点的更多相关文章

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

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

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

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

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

  5. Leetcode 382. Linked List Random Node

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

  6. 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(蓄水池采样)

    1. 问题 给定一个单链表,随机返回一个结点,要求每个结点被选中的概率相等. 2. 思路 在一个给定长度的数组中等概率抽取一个数,可以简单用随机函数random.randint(0, n-1)得到索引 ...

  8. Java实现 LeetCode 382 链表随机节点

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

  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. Linux下汇编语言学习笔记33 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  2. Ubuntu 12.04 之 LAMP

    搭建LAMP环境 (1)更新软件列表: sudo apt-get update 结果报错: W: 无法下载 bzip2:/var/lib/apt/lists/partial/cn.archive.ub ...

  3. HDU 5950 Recursive sequence 递推转矩阵

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. HashSet源码分析2

    package com.test1; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public ...

  5. Vue.js组件的通信之父组件向子父组件的通信

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. swift container server 莫名stuck

    openstack swift container server的进程经常莫名其妙进入 D Ds等状态 记录一下这个时候 storage.error的log 便于分析 一种情形是下面这种log Jun ...

  7. Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可。

    Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可.

  8. i18n国际化的例子

    这个可以点击菜单进行中英文切换,每次切换就可以改变sessionStorage.languge,进行改变i18n的参数lang的值,然后重新调用下就可以了. 工程结构: i18n--| |---css ...

  9. IE6\7\8 :last-child 和 :first-chlid 兼容

    IE9以下不支持last-child ,只支持first-child,边框尽量用上边框.

  10. web前端和后端的区别

    一句话,展示ui相关的就是前端,否则就是后端. 前端语言:javascript.css和html. 后端就是一些服务.