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 #649 (Div. 2) A. XXXXX
题目链接:https://codeforces.com/contest/1364/problem/A 题意 找出大小为 $n$ 的数组 $a$ 的最长连续子数组,其元素和不被 $x$ 整除. 题解 如 ...
- bitbar 网站攻击实验
实验环境 https://github.com/TouwaErioH/security/tree/master/web1 Windows10 Oracle VM VirtualBox Ubuntu16 ...
- 缓冲区溢出实验 1 strcpy
实验代码 https://github.com/TouwaErioH/security/tree/master/stack%20overflow 实验目的 Buffer over flow 漏洞利用实 ...
- C/C++程序内存的各种变量存储区域和各个区域详解
转自 https://blog.csdn.net/jirryzhang/article/details/79518408 C语言在内存中一共分为如下几个区域,分别是: 1. 内存栈区: 存放局部变量名 ...
- QXDM和QCAT软件使用指南
一.传送门 链接:https://pan.baidu.com/s/1i55YXnf 密码:v6nw 二.QXDM,QPST和QCAT的简单说明 QXDM,QPST和QCAT是Qualcomm高通公司针 ...
- JS编程练习:将目标节点内部的子节点逆序
将目标节点内部的子节点逆序 1 <body> 2 <div> 3 <p></p> 4 <span></span> 5 <e ...
- Storybook 最新教程
Storybook 最新教程 Storybook is the most popular UI component development tool for React, Vue, and Angul ...
- API & YApi
API & YApi 接口管理服务 YApi http://yapi.demo.qunar.com/ https://ued.qunar.com/ build bug https://gith ...
- node.js 中间件
node.js 中间件 node.js middleware Express middleware body-parser cookie-parser cookie-session cors csur ...
- JavaScript Functional Programming
JavaScript Functional Programming JavaScript 函数式编程 anonymous function https://en.wikipedia.org/wiki/ ...