这题参照http://blog.jobbole.com/42550/

用的蓄水池算法,即更改ans的概率为1/(当前length)

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution { private 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. */
public Solution(ListNode head) {
this.head = head;
} /** Returns a random node's value. */
public int getRandom() {
ListNode p = head;
int ans = p.val;
for (int i = 1; p.next != null; i++) {
p = p.next;
if ((int)(Math.random() * (i + 1)) == i)
ans = p.val;
}
return ans;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/

LeetCode: Linked List Random Node的更多相关文章

  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 382. Linked List Random Node

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

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

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

  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. 一些SQL

    复制当前所有,ID自动增长.       insert into test(name,pass) select name,pass from test 删除当前重复      delete A fro ...

  2. CodeForces 515C. Drazil and Factorial

    C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. bzoj3212 pku3468 A Simple Problem with Integers

    一个有初值的数列.区间加.区间查 用线段树直接水过 然而并没有1A,主要是做题太快没看规模结果没注意线段树要用longlong建 卧槽怎么可以这么坑爹,害得我看见wa心慌了,还以为连线段树都要跪 一开 ...

  4. Qt里获取目录的一个另类方法

    如果有一个文件的全路径文件名, 想获取它的路径的话, qt里我没找到比较好的办法, 都是cleanPath后, 再用QString的find, left这种函数来处理. 今天又在搞这种问题的时候, 看 ...

  5. TranslateAnimation参数

    看TranslateAnimation动画参数,一直忘记四个参数意思: public    TranslateAnimation(float fromXDelta, float toXDelta, f ...

  6. android——判断网络状态

    平常我们玩qq的时候我们没联网 ,qq上面回显示"网络连接不给力,请检查网络连接"那么它是 通过什么方式判断网络是否连接的呢? 下面将写个案例展示如何检查网络连接状态的 由于我们这 ...

  7. 关于iis8.5中发布的网站无法连接数据库的解决方案。

    发布的网站在浏览时出现如下提示: “/”应用程序中的服务器错误. 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL ...

  8. 用户点击确认登录,自动跳转下面地址得到code

    PHP获取微信openid 简单教程 WEB   2014年10月29日  10868浏览  6评论 获取code https://open.weixin.qq.com/connect/oauth2/ ...

  9. 奥迪--A3

    -型号:A3 -价格:18-28W -动力:1.4T/1.8T -变速箱:7挡双离合 -长宽高:4.32,1.79,1.43(Limousine:4.46,1.80,1.42) -油箱:50L -发动 ...

  10. 一起来做chrome扩展《AJAX请求》

    chrome在一次更新之后,出于安全考虑,完全的禁止了content_script从https向http发起ajax请求,即使正常情况下也会在console里给出提示.这对于WEB来讲是好事,但对于扩 ...