左神算法基础班5_1设计RandomPool结构
Problem:
设计RandomPool结构
【题目】 设计一种结构,在该结构中有如下三个功能:
insert(key):将某个key加入到该结构,做到不重复加入。
delete(key):将原本在结构中的某个key移除。
getRandom():等概率随机返回结构中的任何一个key。
【要求】 Insert、delete和getRandom方法的时间复杂度都是O(1)
Solution:
使用两个hash表,一个是记录标号,一个记录关键词
这里有个关键之处就是,等概率返回一个关键词
若简单使用用哈希表来进行存储,那么有个问题,当删除数据时,会使得哈希表中间产生空白数据
最好的避免中间产生空数据的方法就是将要删除数据的与表中末尾的数据进行交换
然后直接删除最后的数据,故需要使用两个哈希表
Code:
#pragma once
#include <iostream>
#include <hash_map>
#include <string>
#include <time.h>
#include <stdlib.h> using namespace std; template<class T>
class RandomPool
{
public:
void insert(T key);
void del(T key);
T getRandom();
void getPrint(T key);
void getPrint(int index); private:
hash_map<T, int>KeyMap;
hash_map<int, T>IndexMap;
int size = ;
}; template<class T>
void RandomPool<T>::insert(T key)
{
if (KeyMap.find(key) == KeyMap.end())
{
KeyMap[key] = this->size;
IndexMap[this->size] = key;
++(this->size);
cout << "add succeed!" << endl;
}
else
cout << "add filed!" << endl;
} template<class T>
void RandomPool<T>::del(T key)
{
auto ptr = KeyMap.find(key);
if (ptr == KeyMap.end())
{
cout << "delete filed! there is not exsite the key!" << endl;
return;
}
//交换查找到元素与最后一个元素
T temp = IndexMap[--(this->size)];//最后一个元素的关键词,同时将hash表中的元素删除了
int index = KeyMap[key];//要删除元素的位置
KeyMap[temp] = index;
IndexMap[index] = temp;//将最后一个元素替换要删除元素的位置
//正式删除
KeyMap.erase(ptr);
IndexMap.erase(IndexMap.find(index));
} template<class T>
T RandomPool<T>::getRandom()
{
if (this->size == )
{
cout << "the map is empty!" << endl;
}
else
{
int index = (int)((rand() % ( + ) / (double)( + ))*(this->size));//随机生成一个位置
return IndexMap[index];
}
} template<class T>
void RandomPool<T>::getPrint(T key)
{
if (KeyMap.find(key) == KeyMap.end())
cout << "the key is not exsite!" << endl;
else
cout << KeyMap[key] << endl;
} template<class T>
void RandomPool<T>::getPrint(int index)
{
if (IndexMap.find(index) == IndexMap.end())
cout << "the key is not exsite!" << endl;
else
cout << IndexMap[index] << endl;
} void Test()
{
srand((unsigned)time(NULL));
RandomPool<string>map;
map.insert("zz");
map.insert("zw");
map.insert("ww");
map.insert("wz"); cout << map.getRandom() << endl;
map.getPrint();
map.getPrint("ww");
map.del("zw");
map.getPrint("zw");
}
左神算法基础班5_1设计RandomPool结构的更多相关文章
- 左神算法基础班4_1&2实现二叉树的先序、中序、后序遍历,包括递归方式和非递归
Problem: 实现二叉树的先序.中序.后序遍历,包括递归方式和非递归方式 Solution: 切记递归规则: 先遍历根节点,然后是左孩子,右孩子, 根据不同的打印位置来确定中序.前序.后续遍历. ...
- 左神算法进阶班5_4设计可以变更的缓存结构(LRU)
[题目] 设计一种缓存结构,该结构在构造时确定大小,假设大小为K,并有两个功能: set(key, value):将记录(key, value)插入该结构. get(key):返回key对应的valu ...
- 左神算法进阶班1_5BFPRT算法
在无序数组中找到第k大的数1)分组,每N个数一组,(一般5个一组)2)每组分别进行排序,组间不排序3)将每个组的中位数拿出来,若偶数,则拿上 / 下中位数, 成立一个一个新数组.4)新数组递归调用BF ...
- 左神算法进阶班3_1构造数组的MaxTree
题目 一个数组的MaxTree定义: 数组必须没有重复元素 MaxTree是一棵二叉树,数组的每一个值对应一个二叉树节点 包括MaxTree树在内且在其中的每一棵子树上,值最大的节点都是树的头 给定一 ...
- 左神算法进阶班1_4Manacher算法
#include <iostream> #include <string> using namespace std; //使用manacher算法寻找字符中最长的回文子串 in ...
- 左神算法进阶班1_1添加最少字符得到原字符N次
Problem: 给定一个字符串str1,只能往str1的后面添加字符变成str2. 要求1:str2必须包含两个str1,两个str1可以有重合,但是不能以同一个位置开头. 要求2:str2尽量短最 ...
- 左神算法进阶班4_2累加和为aim的最长子数组
[题目] 给定一个数组arr,和一个整数aim,求在arr中,累加和等于num的最长子数组的长度 例子: arr = { 7,3,2,1,1,7,7,7 } aim = 7 其中有很多的子数组累加和等 ...
- 左神算法进阶班8_1数组中累加和小于等于aim的最长子数组
[题目] 给定一个数组arr,全是正数:一个整数aim,求累加和小于等于aim的,最长子数组,要求额外空间复杂度O(1),时间复杂度O(N) [题解] 使用窗口: 双指针,当sum <= aim ...
- 左神算法进阶班6_1LFU缓存实现
[题目] LFU也是一个著名的缓存算法,自行了解之后实现LFU中的set 和 get 要求:两个方法的时间复杂度都为O(1) [题解] LFU算法与LRU算法很像 但LRU是最新使用的排在使用频率最前 ...
随机推荐
- cin,cout优化
https://www.cnblogs.com/PrayG/p/5749832.html ios_base::sync_with_stdio(0); cin.tie(0); 涨知识了
- 从psd图中将图层导出成单独文件
- 程序‘vim’已包含在下列软件包中
解决方法: 输入:sudo apt-get install ctags 输入:sudo apt-get install vim 输入:sudo ./config.sh (亲测有效)输入:vim tes ...
- JZOJ5153:树形图求和
Description Input Output HINT 题解: 一种很直观的想法是通过矩阵生成树求树形图方法数ans以及不包含某一条边i的树形图方法数ans[i],则答案为Σ(ans-ans[i] ...
- NX二次开发-UFUN设置透明度UF_OBJ_set_translucency
NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> UF_initialize( ...
- params拦截器
1. params拦截器首先给action中的相关参数赋值,如id 2. prepare拦截器执行prepare方法,prepare方法中会根据参数,如id,去调用业务逻辑,设置model对象 ...
- gnome/KDE安装,gnome出现问题,重新安装nvdia驱动, Linux(CentOS7) NVIDIA GeForece GTX 745 显卡驱动
新安装显示gtx745驱动NVIDIA-Linux-x86_64-346.59.run, yum groupremove kde-desktop yum groupinstall "Desk ...
- faster-rcnn代码阅读-proposal层
这一节讲述proposal层,和这一层有关的结构图如下: proposal层的prototxt定义如下: layer { name: 'proposal' type: 'Python' bottom: ...
- APC注入DLL(win7下有问题)
void APCKernelRoutine(PKAPC pKAPC, PKNORMAL_ROUTINE pUserAPC, PVOID pContext, PVOID pSysArg1, PVOID ...
- 报错:[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the paren
今天在做Vue的时候,子组件关闭的时候,报如下错误 报错:vue.esm.js?65d7:610 [Vue warn]: Avoid mutating a prop directly since th ...