<Random>382 380
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的更多相关文章
- LeetCode分类-前400题
1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...
- echarts demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- <Random> 380 381(hard) 138
380. Insert Delete GetRandom O(1) class RandomizedSet { ArrayList<Integer> nums; HashMap<In ...
- [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未知. 其 ...
- 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 ...
- 382. Linked List Random Node(蓄水池采样)
1. 问题 给定一个单链表,随机返回一个结点,要求每个结点被选中的概率相等. 2. 思路 在一个给定长度的数组中等概率抽取一个数,可以简单用随机函数random.randint(0, n-1)得到索引 ...
- 382 Linked List Random Node 链表随机节点
给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样.进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现?示例:// 初始化一个单链表 ...
随机推荐
- python 利用cip.cc查询IP归属地
def ipinfocip(ip): # 获得 输入框中的信息 url = "http://www.cip.cc/%s" % ip # 模拟浏览器请求网络 headers={'Us ...
- vue中toggle切换的3种写法
前言:查看下面代码,在任意编辑器中直接复制粘贴运行即可 1:非动态组件(全局注册2个组件,借用v-if指令和三元表达式) <!DOCTYPE html> <html> < ...
- Linux 搭建邮件服务器
一.概述: 在配置邮件服务器之前,先解释几个概念. 1.SMTP:简单邮件传输协议 (Simple Mail Transfer Protocol, SMTP) 通常使用Email都很容易,但是Inte ...
- Linux RAID 磁盘管理
Linux RAID 磁盘管理 RAID工作模式介绍:https://www.cnblogs.com/xiangsikai/p/8441440.html 本章主要讲解 Linux下 RAID5 与 R ...
- C#下的时间测试(用于计算方法执行时间)
public class Timing { private TimeSpan m_StartTime; private TimeSpan duringTime; public Timing() //构 ...
- Android App自动化测试实战(基于Python)(三)
1.Native App自动化测试及Appuim框架介绍 android平台提供了一个基于java语言的测试框架uiautomator,它一个测试的Java库,包含了创建UI测试的各种API和执行自动 ...
- 浅析Java中线程组(ThreadGroup类)
Java中使用ThreadGroup类来代表线程组,表示一组线程的集合,可以对一批线程和线程组进行管理.可以把线程归属到某一个线程组中,线程组中可以有线程对象,也可以有线程组,组中还可以有线程,这样的 ...
- Redis命令geoXXX
1. Redis命令geoXXX 1.1. 介绍 自Redis 3.2开始,Redis基于geohash和有序集合提供了地理位置相关功能. Redis Geo模块包含了以下6个命令: GEOADD: ...
- maven 学习---Maven依赖机制
在 Maven 依赖机制的帮助下自动下载所有必需的依赖库,并保持版本升级. 案例分析 让我们看一个案例研究,以了解它是如何工作的.假设你想使用 Log4j 作为项目的日志.这里你要做什么? 1.在传统 ...
- 【原】Spring测试类代码
package test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.bea ...