381. O(1) 时间插入、删除和获取随机元素 - 允许重复
381. O(1) 时间插入、删除和获取随机元素 - 允许重复
LeetCode_381
题目详情

题解分析

代码实现
package com.walegarrett.interview;
import java.util.*;
/**
* @Author WaleGarrett
* @Date 2021/2/28 22:23
*/
/**
* 题目详情:设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构。
* 注意: 允许出现重复元素。
* insert(val):向集合中插入元素 val。
* remove(val):当 val 存在时,从集合中移除一个 val。
* getRandom:从现有集合中随机获取一个元素。每个元素被返回的概率应该与其在集合中的数量呈线性相关。
*/
public class LeetCode_381 {
List<Integer> list;//存储所有元素
HashMap<Integer, Set<Integer>> map;//存储相同元素在list中的下标
/** Initialize your data structure here. */
public LeetCode_381() {
list = new ArrayList<>();
map = new HashMap<>();
}
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
public boolean insert(int val) {
list.add(val);
Set<Integer> set = map.getOrDefault(val, new HashSet<Integer>());
set.add(list.size()-1);//将val在list中的下标信息存入set中
map.put(val, set);
return set.size() == 1;
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
if(!map.containsKey(val))
return false;
Iterator<Integer> it = map.get(val).iterator();
int index = it.next();//获取待删除元素在map中的下标
//获取list中的最后一个元素
int last = list.get(list.size()-1);
//将list中的index位置的数字变成last
list.set(index, last);
//删除map中val和last的位置信息
map.get(val).remove(index);
map.get(last).remove(list.size()-1);
//将新的位置信息加入到last中
if(index < list.size()-1){
map.get(last).add(index);
}
//如果删除val后list中不存在该元素,则需要从map中移除
if(map.get(val).size() == 0){
map.remove(val);
}
//将调到最后的元素删除
list.remove(list.size()-1);
return true;
}
/** Get a random element from the collection. */
public int getRandom() {
int len = list.size();
return list.get((int)(Math.random() * len));
}
}
381. O(1) 时间插入、删除和获取随机元素 - 允许重复的更多相关文章
- Java实现 LeetCode 381 O(1) 时间插入、删除和获取随机元素 - 允许重复
381. O(1) 时间插入.删除和获取随机元素 - 允许重复 设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构. 注意: 允许出现重复元素. insert(val):向集合中插 ...
- LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed O(1) 时间插入、删除和获取随机元素 - 允许重复(C++/Java)
题目: Design a data structure that supports all following operations in averageO(1) time. Note: Duplic ...
- Leetcode 381. O(1) 时间插入、删除和获取随机元素 - 允许重复
1.题目描述 设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构. 注意: 允许出现重复元素. insert(val):向集合中插入元素 val. remove(val):当 va ...
- 381 Insert Delete GetRandom O(1) - Duplicates allowed O(1) 时间插入、删除和获取随机元素 - 允许重复
设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构.注意: 允许出现重复元素. insert(val):向集合中插入元素 val. remove(val):当 val ...
- [Swift]LeetCode381. O(1) 时间插入、删除和获取随机元素 - 允许重复 | Insert Delete GetRandom O(1) - Duplicates allowed
Design a data structure that supports all following operations in averageO(1) time. Note: Duplicate ...
- LeetCode380 常数时间插入、删除和获取随机元素
LeetCode380 常数时间插入.删除和获取随机元素 题目要求 设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构. insert(val):当元素 val 不存在时,向集合中插 ...
- Java实现 LeetCode 380 常数时间插入、删除和获取随机元素
380. 常数时间插入.删除和获取随机元素 设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构. insert(val):当元素 val 不存在时,向集合中插入该项. remove( ...
- LeetCode 380. Insert Delete GetRandom O(1) 常数时间插入、删除和获取随机元素(C++/Java)
题目: Design a data structure that supports all following operations in averageO(1) time. insert(val): ...
- LeetCode 哈希表 380. 常数时间插入、删除和获取随机元素(设计数据结构 List HashMap底层 时间复杂度)
比起之前那些问计数哈希表的题目,这道题好像更接近哈希表的底层机制. java中hashmap的实现是通过List<Node>,即链表的list,如果链表过长则换为红黑树,如果容量不足(装填 ...
随机推荐
- Codeforces Round #643 (Div. 2) 题解 (ABCDE)
目录 A. Sequence with Digits B. Young Explorers C. Count Triangles D. Game With Array E. Restorer Dist ...
- log4net GetLogger(source).IsInfoEnabled = false
GetLogger(source).IsInfoEnabled = false解决办法 在.net core中需要把log4net.config放到 ITCP.Web\ITCP.Web\obj\Rel ...
- 使用 Tye 辅助开发 k8s 应用竟如此简单(二)
续上篇,这篇我们来进一步探索 Tye 更多的使用方法.本篇我们来了解一下如何在 Tye 中使用服务发现. Newbe.Claptrap 是一个用于轻松应对并发问题的分布式开发框架.如果您是首次阅读本系 ...
- ElasticSearch 搜索引擎概念简介
公号:码农充电站pro 主页:https://codeshellme.github.io 1,倒排索引 倒排索引是一种数据结构,经常用在搜索引擎的实现中,用于快速找到某个单词所在的文档. 倒排索引会记 ...
- Java 在Word中添加多行图片水印
Word中设置水印效果时,不论是文本水印或者是图片水印都只能添加单个文字或者图片到Word页面,效果比较单一,本文通过Java代码示例介绍如何在页面中添加多行图片水印效果,即水印效果以多个图片平铺到页 ...
- 【转】Dockerfile
1. 关于docker build docker build可以基于Dockerfile和context打包出一个镜像,其中context是一系列在PATH或URL中指定的位置中的文件(contex ...
- Linux虚拟机封装成模板
对安装在VMware上的CentOS7.X进行封装,是为了后续的实验环境需要,可以批量去生成Linux系统.通过虚拟机模版来创建一台CentOS系统,跟原来机器一样,去掉了唯一性,而通过克隆出来的虚拟 ...
- 操作系统 part2
一.程序的内存结构 references: newcoder 运行时,程序分为:text段.data段.BSS段(2个合称数据段).堆.栈. text段:代码段,静态分配内存,只读. data段:初始 ...
- JavaScript常见笔试题分析
1.Javascript的typeof可能返回的结果有哪些? 答:共6种,具体为number ,boolean,string,undefined,function,object(对象或者null返 ...
- H5 Funny Games All In One
H5 Funny Games All In One H5 游戏 盖楼 游戏 https://iamkun.github.io/tower_game/ https://github.com/iamkun ...