382. Linked List Random Node

class Solution {
ListNode node;
Random random;
/** @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) {
node = head;
random = new Random();
} /** Returns a random node's value. */
public int getRandom() {
ListNode candidate = node;
int result = candidate.val;
int count = 0;
while(true){
if(candidate == null) break;
if(random.nextInt(++count) == 0) result = candidate.val;
candidate = candidate.next;
}
return result;
}
}

380. Insert Delete GetRandom O(1)

class RandomizedSet {
ArrayList<Integer> nums;
HashMap<Integer, Integer> locs;
Random rand = new Random();
/** Initialize your data structure here. */
public RandomizedSet() {
nums = new ArrayList<Integer>();
locs = new HashMap<Integer, Integer>();
} /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public boolean insert(int val) {
boolean contain = locs.containKey(val);
if( contain ) return false;
locs.put(val, nums.size());
nums.add(val);
return true;
} /** Removes a value from the set. Returns true if the set contained the specified element. */
public boolean remove(int val) {
boolean contain = locs.containKey(val);
if( !contain ) return false;
int loc = locs.get(val);
if(loc < nums.size() - 1){// not the last one than swap the last one with this val
int lastOneVal = nums.get(nums.size() - 1);
nums.set(loc, lastOneVal);
locs.put(lastOneVal, loc);
}
locs.remove(val);
nums.remove(nums.size() - 1);
return true;
} /** Get a random element from the set. */
public int getRandom() {
retrn nums.get(rand.nextInt(num.size()));
}
}

<Random>382 380的更多相关文章

  1. LeetCode分类-前400题

    1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...

  2. echarts demo

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. <Random> 380 381(hard) 138

    380. Insert Delete GetRandom O(1) class RandomizedSet { ArrayList<Integer> nums; HashMap<In ...

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

  8. 382. Linked List Random Node(蓄水池采样)

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

  9. 382 Linked List Random Node 链表随机节点

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

随机推荐

  1. Wwise音频解决方案概述

    Wwise(Wave Works Interactive Sound Engine,Wwise基础知识,wiki)是Audiokinetic公司提供的跨平台游戏音频解决方案,有着高效完整工作流和工具链 ...

  2. spring cloud hystrix dashboard 没有/actuator/hystrix.stream路径解决

    首先我用的是spring boot Greenwich.SR2 在测试hystrix-dashboard监控服务时,发现访问localhost:9001/actuator/hystrix.stream ...

  3. 腾讯微视:向前一步是悲壮,向后一步是绝望zz

    “换做以往的任何一场战争,微博之战.搜索之战和电商之战——大量投入但始终不见效果,打到现在腾讯肯定已经交牌了.微视的不同之处在于,它有些悲壮,因为这是腾讯最不想交出的一张牌.” 文 | <财经& ...

  4. jQuery 源码解析(七) jQuery对象和DOM对象的互相转换

    jQuery对象是一个类数组对象,它保存的是对应的DOM的引用,我们可以直接用[]获取某个索引内的DOM节点,也可以用get方法获取某个索引内的DOM节点,还可以用toArray()方法把jQuery ...

  5. 知识图谱与Bert结合

    论文题目: ERNIE: Enhanced Language Representation with Informative Entities(THU/ACL2019) 本文的工作也是属于对BERT锦 ...

  6. dedecms5.7的获取本文章的TAG

    tag调用标签如下: {dede:tag row='10' getall='1' sort='month'} <li><a href='[field:link/]'>[fiel ...

  7. VS2017新建项目的模板之配置

    也不知道之前装VS2017的时候,做了什么操作,新建一个WinForm项目,自动记住了我当时新建的窗体的大小816*639(默认的300*300),现在每次新建窗体都这个大小,忍了一段时间,实在忍无可 ...

  8. 使用Debug Diagnostic Tool排除内存泄漏故障

    在我之前的博文中(SQL Server内存泄漏),我解释了如何使用“!heap”命令识别哪个模块泄漏了内存.有时我们使用“!d”命令来找到模型或者使用搜索内存命令(s)不能通过显示内存找到原因. 在这 ...

  9. Windows Socket知识总结

     目录 0 理解Socket 1 WinSock API 2 阻塞socket 3 非阻塞Socket 4 套接字IO模型  4.1 套接字IO模型:select(选择)  4.2 套接字IO模型:W ...

  10. SAP MM 公司间STO里外向交货单与内向交货单里序列号对应关系

    SAP MM 公司间STO里外向交货单与内向交货单里序列号对应关系 笔者所在的A项目,后勤模块里有启用HU管理,序列号管理,批次管理等功能,以实现各个业务场景下的追溯. 公司间转储订单流程里,如果是整 ...