넘치게 채우기

[LeetCode] 705. Design HashSet 본문

PS/LeetCode

[LeetCode] 705. Design HashSet

riveroverflow 2023. 5. 30. 10:19
728x90
반응형

 

https://leetcode.com/problems/design-hashset/description/

 

Design HashSet - LeetCode

Can you solve this real interview question? Design HashSet - Design a HashSet without using any built-in hash table libraries. Implement MyHashSet class: * void add(key) Inserts the value key into the HashSet. * bool contains(key) Returns whether the value

leetcode.com

문제 유형 : 해시

문제 난이도 : Easy

 

Design a HashSet without using any built-in hash table libraries.

Implement MyHashSet class:

  • void add(key) Inserts the value key into the HashSet.
  • bool contains(key) Returns whether the value key exists in the HashSet or not.
  • void remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.

라이브러리 없이 간단한 해시 집합을 구현하라.

 

 

풀이

배열을 하나 만들어서 구현해주면 된다

 

코드(C++)

class MyHashSet {
public:
    vector<int> set;

    MyHashSet() {
    }
    
    void add(int key) {
        set.push_back(key);
    }
    
    void remove(int key) {
        set.erase(std::remove(set.begin(), set.end(), key), set.end());
    }
    
    bool contains(int key) {
        return count(set.begin(), set.end(), key) > 0;
    }
};
728x90
반응형