设计RandomPool结构
2018-03-15 20:49
316 查看
设计RandomPool结构
【题目】 设计一种结构,在该结构中有如下三个功能:
insert(key):将某个key加入到该结构,做到不重复加入。
delete(key):将原本在结构中的某个key移除。 getRandom():
等概率随机返回结构中的任何一个key。
【要求】 Insert、delete和getRandom方法的时间复杂度都是O(1)import java.util.HashMap;
import java.util.Random;
public class C01_RandomPoll {
public static class Pool<Key>{
//HashMap<K, String>这里的K表示只要是同一种类型就OK了,但并没有指定是哪一种类型
//例如我需要放String的类型,那第一个放进去的时候,K就被指定为String 了
//理论上是更为强大的
HashMap<Key, Integer>keyIndexMap;
HashMap<Integer, Key>indexKeyMap;
int size;
public Pool(){
keyIndexMap = new HashMap<Key, Integer>();
indexKeyMap = new HashMap<Integer, Key>();
}
//插入到pool
public void insert(Key k){
keyIndexMap.put(k, size);
indexKeyMap.put(size++, k);
}
//删除(这个结合文末的图片提示来看)
public void delete(Key key){
if(!keyIndexMap.containsKey(key)){
return ;
}
int lastIndex = --this.size;
int deleteIndex = keyIndexMap.get(key);
Key lastKey = indexKeyMap.get(lastIndex);
keyIndexMap.put(lastKey, deleteIndex);
indexKeyMap.put(deleteIndex, lastKey);
keyIndexMap.remove(lastKey);
}
//获取随机
public Key getRandom(){
if(size==0){
return null;
}
Random random = new Random();
int index = random.nextInt(keyIndexMap.size());
return indexKeyMap.get(index);
}
//test
public static void main(String[] args) {
Pool<String> pool = new Pool<String>();
pool.insert("laoganma");
pool.insert("nengandie");
pool.insert("yyziii");
pool.insert("aaaaaa");
for (int i = 0; i < 10; i++) {
System.out.println(pool.getRandom());
}
pool.delete("nengandie");
System.out.println("=======");
for (int i = 0; i < 10; i++) {
System.out.println(pool.getRandom());
}
}
}
}
【题目】 设计一种结构,在该结构中有如下三个功能:
insert(key):将某个key加入到该结构,做到不重复加入。
delete(key):将原本在结构中的某个key移除。 getRandom():
等概率随机返回结构中的任何一个key。
【要求】 Insert、delete和getRandom方法的时间复杂度都是O(1)import java.util.HashMap;
import java.util.Random;
public class C01_RandomPoll {
public static class Pool<Key>{
//HashMap<K, String>这里的K表示只要是同一种类型就OK了,但并没有指定是哪一种类型
//例如我需要放String的类型,那第一个放进去的时候,K就被指定为String 了
//理论上是更为强大的
HashMap<Key, Integer>keyIndexMap;
HashMap<Integer, Key>indexKeyMap;
int size;
public Pool(){
keyIndexMap = new HashMap<Key, Integer>();
indexKeyMap = new HashMap<Integer, Key>();
}
//插入到pool
public void insert(Key k){
keyIndexMap.put(k, size);
indexKeyMap.put(size++, k);
}
//删除(这个结合文末的图片提示来看)
public void delete(Key key){
if(!keyIndexMap.containsKey(key)){
return ;
}
int lastIndex = --this.size;
int deleteIndex = keyIndexMap.get(key);
Key lastKey = indexKeyMap.get(lastIndex);
keyIndexMap.put(lastKey, deleteIndex);
indexKeyMap.put(deleteIndex, lastKey);
keyIndexMap.remove(lastKey);
}
//获取随机
public Key getRandom(){
if(size==0){
return null;
}
Random random = new Random();
int index = random.nextInt(keyIndexMap.size());
return indexKeyMap.get(index);
}
//test
public static void main(String[] args) {
Pool<String> pool = new Pool<String>();
pool.insert("laoganma");
pool.insert("nengandie");
pool.insert("yyziii");
pool.insert("aaaaaa");
for (int i = 0; i < 10; i++) {
System.out.println(pool.getRandom());
}
pool.delete("nengandie");
System.out.println("=======");
for (int i = 0; i < 10; i++) {
System.out.println(pool.getRandom());
}
}
}
}
相关文章推荐
- 其他题目---设计RandomPool结构
- 【哈希表】实现结构RandomPool,用O(1)时间完成插入、删除和随机返回的功能
- 完成端口中的单句柄数据结构与单IO数据结构的理解与设计
- 3.5 实例讲解Lucene索引的结构设计
- 设计模式与Spring的MVC结构
- [Java]数据结构课程设计:迷宫老鼠2.0(完整项目代码与设计报告)
- 设计的根本是为用户解决产品的结构问题
- 如何设计好的URL地址结构
- javaAPI深入理解(2)Collection,List的设计,ListIterator与Iterator,RandomAccess,List排序、逆序以及其他
- 一个开源的IoC采集服务器体系结构设计
- JPivot的MVC结构和设计模式
- 领域驱动设计下系统层次结构风格(转载)
- 数据结构课程设计--------文法分析中推导的建立(第一天进度)
- 讲解JavaScript的Backbone.js框架的MVC结构设计理念
- PowerDesigner15创建MySQL数据库表结构设计
- 【露】软件结构设计中。。。
- 结构型:设计模式之组合模式(十)
- Mysql学习之表结构设计、索引、外键与数据插入
- 一种不错的树结构数据表设计
- IT忍者神龟之树形结构的数据库表Schema设计