Insert Delete GetRandom O(1)
2018-07-15 18:36:29
问题描述:

问题求解:
private ArrayList<Integer> ls;
private HashMap<Integer, Integer> map;
private Random rand; /** Initialize your data structure here. */
public RandomizedSet() {
ls = new ArrayList<>();
map = new HashMap<>();
rand = new Random();
} /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public boolean insert(int val) {
if (map.containsKey(val)) return false;
ls.add(val);
map.put(val, ls.size() - 1);
return true;
} /** Removes a value from the set. Returns true if the set contained the specified element. */
public boolean remove(int val) {
if (!map.containsKey(val)) return false;
int idx = map.get(val);
if (idx != ls.size() - 1) {
int temp = ls.get(ls.size() - 1);
ls.set(idx, temp);
map.put(temp, idx);
}
map.remove(val);
ls.remove(ls.size() - 1);
return true;
} /** Get a random element from the set. */
public int getRandom() {
return ls.get(rand.nextInt(ls.size()));
}
Insert Delete GetRandom O(1)的更多相关文章
- [LeetCode] Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- [LeetCode] Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
- LeetCode 380. Insert Delete GetRandom O(1)
380. Insert Delete GetRandom O(1) Add to List Description Submission Solutions Total Accepted: 21771 ...
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- [LeetCode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- [LeetCode] 380. Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
- [LeetCode] 380. Insert Delete GetRandom O(1) 插入删除获得随机数O(1)时间
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
- [LeetCode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 插入删除和获得随机数O(1)时间 - 允许重复
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed
原题链接在这里:https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/?tab=Description ...
- leetcode 380. Insert Delete GetRandom O(1) 、381. Insert Delete GetRandom O(1) - Duplicates allowed
380. Insert Delete GetRandom O(1) 实现插入.删除.获得随机数功能,且时间复杂度都在O(1).实际上在插入.删除两个功能中都包含了查找功能,当然查找也必须是O(1). ...
随机推荐
- git克隆代码
1.vs--team explorer-clone,或者team-connect to tfs-clone 2.1输入git的url,2输入本地放代码的文件夹,3点clone,克隆出4.双击4 3.点 ...
- mysql表结构文件
- Java(16-19)
0. 正则表达式: str.matches() //判断字符串是否匹配 str.split() // 根据给定正则表达式的匹配规则.拆分此字符串,返回字符串数组. str.replaceAll() ...
- python 用类方法和静态方法实现是追加写文件内容,和读指定行号的内容
用类方法和静态方法实现:一个是追加写文件一行内容,一个是读指定行号的内容 #coding=utf-8 class handle_file(object): def __init__(s ...
- ajax 请求发出了,数据更改了,但是没进入success 函数 把success 换成 complete
$(function(){ $(document).on('tap','.w-location-group .mui-table-view-cell',function(){ var bool = $ ...
- window开机启动项设置和取消方法
window开机启动项1.添加开机启动项:开始-->所有程序-->启动-->双击(xp系统)或右键打开,把需要启动的软件快捷键拖放进去即可,遇到安全软件的拦截,只需选择 " ...
- 如何在Qt Creator中创建pri文件,以及pri文件的说明
初学Qt的人可还不会接触到这个问题,但是一旦你开始编写某个较大项目的时候,这个问题就不可避免需要解决. 对于大神们来讲可能这是个很简单的问题,但是对于新手来说,想要搞清楚需要下很大功夫. 怎么创建pr ...
- 20145205武钰_Exp5 MSF基础应用
20145205武钰_Exp5 MSF基础应用 实验后回答问题 exploit:这个词本身只是利用,但是它在黑客眼里就是漏洞利用.有漏洞不一定就有Exploit(利用).有Exploit就肯定有漏洞. ...
- C++中两个类中互相包含对方对象的指针问题(转载)
出处:http://www.cnblogs.com/hanxi/archive/2012/07/25/2608068.html // A.h #include "B.h" clas ...
- RS(纠删码)技术浅析及Python实现
前言 在Ceph和RAID存储领域,RS纠删码扮演着重要的角色,纠删码是经典的时间换空间的案例,通过更多的CPU计算,降低低频存储数据的存储空间占用. 纠删码原理 纠删码基于范德蒙德矩阵实现,核心公式 ...