<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 链表随机节点
给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样.进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现?示例:// 初始化一个单链表 ...
随机推荐
- Java后台+数据库+Java web前端——记账本
下面是本人实现的网页版(设计思路见上一篇https://www.cnblogs.com/sengzhao666/p/10445984.html) 代码如下: 运行截图: 首页: 创建: 账本删除:(先 ...
- Java 并发系列之十一:并发线程带来的风险
1. 概述 在并发中有两种方式,一是多进程,二是多线程,但是线程相比进程花销更小且能共享资源. 线程带来的风险: 1. 安全性问题.错误的问题永不发生.竞态条件(顺序敏感). 2. 活跃性问题.正确的 ...
- 记录下github 与 gitee 同时使用
参考 Gitee(码云).Github同时配置ssh key 中间有一步,创建config文件,然后测试就过不去了. 报错:Bad owner or permissions on C:\\Users\ ...
- 小米笔记本pro 黑苹果系统无法进入系统,频繁重启故障解决记录
问题1:频繁重启,然后clover丢失 表现情况:开机没有选择macos 或windos的界面 解决办法:进入windows使用工具easyefi,直接添加一个clover start boot,选择 ...
- LeetCode 232:用栈实现队列 Implement Queue using Stacks
题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...
- D3力布图绘制--节点跑掉,单曲线弯曲问题记录
D3力布图绘制中遇到的交互问题,频繁操作数据后,会出现节点跑掉和单曲线弯曲的问题 问题描述 在id指向都正常的情况下出现以下2种状况: 单曲线弯曲 节点跑掉 经排查,是数据重复导致的问题 线条也是一样 ...
- PDF文件添加二维码水印教程
maven配置iText的jar,主要不是所有私服都有iText的jar,maven仓库没有的,可以去https://mvnrepository.com/artifact/com.itextpdf/i ...
- Spring Boot +Bootstrap 图片上传与下载,以及在bootstrap-table中的显示
1.前台上传: <input type="file" name="file" id="file"> 2.后台的接收与处理: St ...
- C#关于函数重载的坑
今天在调用被重载的函数时,发现一个问题 private ProductRegisterResponse InitResponse(int code, string message, string pw ...
- springcloud分布式事务Atomikos实例
0.JTA(Java Transaction Manager)的介绍 (1)jta与jdbc 简单的说 jta是多库的事务 jdbc是单库的事务 (2)XA与JTA XA : XA是一个规范或是一个事 ...