Title:Design HashMap 706
Difficulty:Easy
原题leetcode地址: https://leetcode.com/problems/design-hashmap/
1. 见代码
时间复杂度:O(1)。
空间复杂度:O(n),申请数组。
public class HashTable0706DesignHashMap {
/**
* main
* @param args
*/
public static void main(String args[]) {
}
private final int BLOCK = 1000005;
boolean[] map;
int[] nums;
/** Initialize your data structure here. */
public HashTable0706DesignHashMap() {
map = new boolean[BLOCK];
nums = new int[BLOCK];
}
/** value will always be non-negative. */
public void put(int key, int value) {
map[key] = true;
nums[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) {
if (map[key]) {
return nums[key];
}
else {
return -1;
}
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
public void remove(int key) {
map[key] = false;
}
}