linked-list-random-node
https://leetcode.com/problems/linked-list-random-node/ // Using Reservoir sampling algorithm
// https://discuss.leetcode.com/topic/53812/using-reservoir-sampling-o-1-space-o-n-time-complexity-c/2 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
ListNode h;
java.util.Random rand; /** @param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node. */
public Solution(ListNode head) {
h = head;
rand = new java.util.Random();
} /** Returns a random node's value. */
public int getRandom() {
if (h == null) {
return 0;
} int ret = h.val;
int i = 1;
ListNode cur = h;
while (cur.next != null) {
cur = cur.next;
i++;
int f = rand.nextInt() % i;
if (f == 0) {
ret = cur.val;
}
}
return ret;
}
} /**
* 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] 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 ...
- Leetcode 382. Linked List Random Node
本题可以用reservoir sampling来解决不明list长度的情况下平均概率选择元素的问题. 假设在[x_1,...,x_n]只选一个元素,要求每个元素被选中的概率都是1/n,但是n未知. 其 ...
- 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 ...
- [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 ...
- 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 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 ...
- Linked List Random Node -- LeetCode
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 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
随机推荐
- 把对象转换成JSON形式的html代码
function formatJson(msg) { var rep = "~"; var jsonStr = JSON.stringify(msg, null, rep) var ...
- 使用IDEA运行Eclipse编辑jetty运行的J2EE项目的惨痛教训
公司的项目原本是使用Eclipse,使用自带的jetty运行, 用IDEA通过git clone后,使用Tomcat运行,可以运行,却无法访问页面,总是报错404 后来使用IDEA Jetty运行,经 ...
- noi 2727:仙岛求药
2727:仙岛求药 总时间限制: 1000ms 内存限制: 65536kB 描述 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难 ...
- [BZOJ4881][Lydsy1705月赛]线段游戏
首先冷静一下看清问题的本质,是将整个数列分成两个递增子序列. 那么由Dilworth定理得,无解当且仅当数列的最长下降子序列的长度>2,先特判掉. 然后就有一些比较厉害的做法:http://ww ...
- Minimum Size Subarray Sum 最短子数组之和
题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...
- KMP 理解
例题 以字符串 ABABACA 为例 a 前缀: 后缀: 结果为0 ab 前缀:a 后缀: b 结果为0 aba 前缀:a ab 后缀: ba a 结果为1,此时 i=2,j=1 abab 前缀:a ...
- Codeforces Round #257 (Div. 2) A. Jzzhu and Children
A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces 505A Mr. Kitayuta's Gift 暴力
A. Mr. Kitayuta's Gift time limit per test 1 second memory limit per test 256 megabytes input standa ...
- NOIP 2008 传纸条 NOIP 2000 方块取数 多线程DP
思路都是一样,建立一个四维dp然后跑一发就完了 当然,也可以像我这么帅的人,降成三维再傻傻的跑一发啦啦啦~ #include<iostream> #include<stdio.h&g ...
- [Visual Studio] 安装清单
VS安装位置要求路径必须是英文,且位于Program Files (x86)文件夹下. 下载工具vs_Professional.exe:https://pan.baidu.com/s/1jHRjiia ...