본문 바로가기
자바 코딩테스트

706 leetcode 디자인 hashmap

by 백엔드 개발자 2025. 4. 10.

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

Implement the MyHashMap class:

  • MyHashMap() initializes the object with an empty map.
  • void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
  • int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.

 

put은 key value쌍을 hashMap에 집어넣는다. key가 이미 존재하면 값을 업데이트한다.

get은 key가 매핑되어있는 값을 리턴한다. 값이 없으면 -1을 리턴한다.

remove는 key에 매핑되는 값이 있으면  key에 매핑되는 값을 제거한다. 

 

 

 

심플하게 HashMap을 디자인해보는 것이었다.

put, get, remove를 구현하고 종료

 

class MyHashMap {
    private Map<Integer, Integer> map;
    public MyHashMap() {
        map = new HashMap<>();
    }
    
    public void put(int key, int value) {
        map.put(key, value);   
    }
    
    public int get(int key) {
        if (map.containsKey(key)) {
            return map.get(key);
        }
        return -1;
    }
    
    public void remove(int key) {
        if (map.containsKey(key)) {
            map.remove(key);
        }
    }
}

 

'자바 코딩테스트' 카테고리의 다른 글

리트코드187. Repeated DNA Sequences  (0) 2025.04.14
2358 평행선  (1) 2025.04.12
백준 3986 좋은 단어  (0) 2025.04.08
리트코드 70. Climbing Stairs  (0) 2025.04.07
리트코드 225 큐를 사용해서 스택 구현하기  (0) 2025.04.04