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();
代码如下:(方法一)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution { /** @param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node. */ private List<Integer> list=new ArrayList<>(); public Solution(ListNode head) {
ListNode p=head;
int i=0;
while(p!=null)
{
list.add(p.val);
p=p.next;
i++;
}
} /** Returns a random node's value. */
public int getRandom() {
Random random=new Random(); int q=random.nextInt(list.size());
return list.get(q); }
} /**
* 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.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution { /** @param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node. */ private ListNode list=null;
private int size=0;
public Solution(ListNode head) {
list=head;
ListNode p=head;
while(p!=null)
{
p=p.next;
size++;
}
} /** Returns a random node's value. */
public int getRandom() {
ListNode ss=list;
int rand=(int)(Math.random()*size);
while(rand>0&&ss!=null)
{
ss=ss.next;
rand--;
}
return ss!=null?ss.val:0;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/
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 ☆☆☆
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 链表随机节点
给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样.进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现?示例:// 初始化一个单链表 ...
- 【LeetCode】382. Linked List Random Node 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组保存再随机选择 蓄水池抽样 日期 题目地址:ht ...
- 382. Linked List Random Node(蓄水池采样)
1. 问题 给定一个单链表,随机返回一个结点,要求每个结点被选中的概率相等. 2. 思路 在一个给定长度的数组中等概率抽取一个数,可以简单用随机函数random.randint(0, n-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 ...
- LeetCode: Linked List Random Node
这题参照http://blog.jobbole.com/42550/ 用的蓄水池算法,即更改ans的概率为1/(当前length) /** * Definition for singly-linked ...
- [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 ...
随机推荐
- 类成员函数作为pthread_create函数参数
from:http://www.cnblogs.com/shijingxiang/articles/5389294.html 近日需要将线程池封装成C++类,类名为Threadpool.在类的成员函数 ...
- java程序(一)----HashMap同时获取键值
快速会用: HashMap<Integer,String> maps=new HashMap<Integer,String>(); maps.put(1,"xiaom ...
- jstl c:choose>、<c:when>和<c:otherwise>标签
<c:choose>.<c:when>和<c:otherwise>在一起连用,可以实现Java语言中的if-else语句的功能.例如以下代码根据username请求 ...
- DB2事务日志已满的解决方法
DB2命令终端输入: db2 update db cfg for <dbname> using LOGPRIMARY 50 db2 update db cfg for <dbname ...
- C# WebRequestExtensions
https://gist.github.com/abombss/2720757 public static class WebRequestExtensions { public static Htt ...
- iOS中nil、Nil、NULL、NSNull详解(转)
ObjC 里面的几个空值符号经常会差点把我搞死,这些基础的东西一点要弄清楚才行,以提高码农的基本素质. nil nil 是 ObjC 对象的字面空值,对应 id 类型的对象,或者使用 @interfa ...
- hdu 2051
ps:这道题是题目坑爹了...题目说不考虑n=0的...但其实要考虑...醉了 中文意思:输入一个10进制的数,输出他的二进制数 代码: #include "stdio.h"int ...
- UIkit框架之UIbutton的使用
1.UIbutton的继承关系:UIcontroller:UIview:UIresponder:NSObject: 2.添加按钮的步骤: (1)创建按钮的时候首先设置类型 (2)添加标题或者图片,设置 ...
- device framework(设备框架)
Table A-1 Device frameworks Name First available Prefixes Description Accelerate.framework 4.0 cbla ...
- FR #2题解
A. 考虑把(u,v)的询问离线挂在u上,然后dfs,每次从fath[x]到[x]相当于x子树dis区间加1,x子树以外区间-1,然后维护区间和区间平方和等. 常数略大. #include<io ...