D87 705. Design HashSet
Topic Link
Topic analysis
Design a hash class.
Add additional element needed function, contains the function determines the elements present, remove the function elements are removed.
Thinking
It's really nothing left to say ... I want to keep the value as the key storage array.
The final code
class MyHashSet {
protected $values = [];
/**
* Initialize your data structure here.
*/
function __construct() {
}
/**
* @param Integer $key
* @return NULL
*/
function add($key) {
$this->values[$key] = true;
}
/**
* @param Integer $key
* @return NULL
*/
function remove($key) {
if(isset($this->values[$key])){
unset($this->values[$key]);
}
}
/**
* Returns true if this set contains the specified element
* @param Integer $key
* @return Boolean
*/
function contains($key) {
return isset($this->values[$key]);
}
}
/**
* Your MyHashSet object will be instantiated and called as such:
* $obj = MyHashSet();
* $obj->add($key);
* $obj->remove($key);
* $ret_3 = $obj->contains($key);
*/
复制代码
If you find this article useful, welcomed with love of power assistance.
Reproduced in: https: //juejin.im/post/5d04674951882548ac4399c4