705. escenografía de hash
No utilice ningún tabla hash incorporado en el diseño de la biblioteca un conjunto de hash
En concreto, su diseño debe incluir las siguientes funciones
añadir (valor): inserta un valor para el conjunto de hash.
contiene (valor): Devuelve el valor de la existencia de esta colección de hash.
remove (valor): dado un valor hash se elimina de la colección. Si el valor hash no se establece, no hacer nada.
Ejemplo:
MyHashSet hashSet = new MyHashSet();
hashSet.add(1);
hashSet.add(2);
hashSet.contains(1); // 返回 true
hashSet.contains(3); // 返回 false (未找到)
hashSet.add(2);
hashSet.contains(2); // 返回 true
hashSet.remove(2);
hashSet.contains(2); // 返回 false (已经被删除)
nota:
Todos los valores en el rango [0, 1000000] es.
El número total de operaciones en el intervalo [1, 10000].
No utilice la biblioteca conjunto de hash incorporado.
class MyHashSet {
boolean[] flag;
/** Initialize your data structure here. */
public MyHashSet() {
flag=new boolean[1000001];
}
public void add(int key) {
flag[key]=true;
}
public void remove(int key) {
flag[key]=false;
}
/** Returns true if this set contains the specified element */
public boolean contains(int key) {
return flag[key];
}
}
/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/