LeetCode706(2021.3.14)

LC706. 设计哈希映射

class MyHashMap {

    /**
     * Initialize your data structure here.
     */
    ArrayList<Integer> data;

    public MyHashMap() {
        data = new ArrayList<Integer>();
        for (int i = 0; i < 1000010; ++i) {
            data.add(-1);
        }
    }

    /**
     * value will always be non-negative.
     */
    public void put(int key, int value) {
        data.set(key, value);
    }

    /**
     * Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
     */
    public int get(int key) {
        return data.get(key);
    }

    /**
     * Removes the mapping of the specified value key if this map contains a mapping for the key
     */
    public void remove(int key) {
        data.set(key, -1);
    }
}

留下评论