1.題目
設計RandomPool結構 【題目】 設計一種結構,在該結構中有如下三個功能:
insert(key):將某個key加入到該結構,做到不重複加入。 delete(key):將原本在結構中的某個key移除。
getRandom(): 等概率隨機返回結構中的任何一個key。 【要求】
Insert、delete和getRandom方法的時間複雜度都是 O(1)
2.思路
題目要求必須是O(1)的時間複雜度,那麼上哈希表就可以了,但是隨機返回等概率的key 哈希表是做不到的,
因此,我們可以藉助兩個hash表來做
比如 添加數據
hash1 key value hash2 key value
z 1 1 z
x 2 2 x
y 3 3 y
hash1記錄的是key的值 value 記錄當前數據的大小, 而key是唯一的,所以可以保證數據的唯一性。
hash2 key 記錄的是數據的大小,由於添加數據size不斷變化 所以可以保證唯一,在使用一個random函數 隨機
圖解
3.code
package com.ncst.base.four;
import java.util.HashMap;
/**
* @author i
* @create 2020/5/19 15:52
* @Description
*
* 設計RandomPool結構
* 【題目】 設計一種結構,在該結構中有如下三個功能:
* insert(key):將某個key加入到該結構,做到不重複加入。
* delete(key):將原本在結構中的某個key移除。 getRandom():
* 等概率隨機返回結構中的任何一個key。
* 【要求】 Insert、delete和getRandom方法的時間複雜度都是 O(1)
題目要求必須是O(1)的時間複雜度,那麼上哈希表就可以了,但是隨機返回等概率的key 哈希表是做不到的,
因此,我們可以藉助兩個hash表來做
比如 添加數據
hash1 key value hash2 key value
z 1 1 z
x 2 2 x
y 3 3 y
hash1記錄的是key的值 value 記錄當前數據的大小, 而key是唯一的,所以可以保證數據的唯一性。
hash2 key 記錄的是數據的大小,由於添加數據size不斷變化 所以可以保證唯一,在使用一個random函數 隨機
*
*/
public class RandomPool {
private HashMap<String,Integer> hash1;
private HashMap<Integer,String> hash2;
private Integer size;
public RandomPool(){
hash1 = new HashMap<>();
hash2 = new HashMap<>();
size = 0;
}
public void put(String key){
if(!hash1.containsKey(key)){
hash1.put(key,size);
hash2.put(size++,key);
}
}
public String random(){
if (!hash1.isEmpty()){
Integer randomIndex = (int)Math.random()*size+1;
return hash2.get(randomIndex);
}
return null;
}
public void remove(String key){
if (hash1.containsKey(key)){
//1.先找到要刪除的下標
Integer index = hash1.get(key);
//2.size-- 獲取到當前hash表中最後一個數據
Integer maxIndex = --size;
//3.通過上一步獲取到最後一個表的數據的size多少。
String s = hash2.get(maxIndex);
//4.然後將最後一個元素添加到index的位置
hash1.put(s,index);
hash2.put(index,s);
hash1.remove(key);
hash2.remove(maxIndex);
//
}
}
public static void main(String[] args) {
RandomPool randomPool = new RandomPool();
randomPool.put("a");
randomPool.put("b");
randomPool.put("c");
System.out.println(randomPool.random());
randomPool.remove("b");
System.out.println();
}
}