-
Notifications
You must be signed in to change notification settings - Fork 889
/
DesignHashMap.swift
62 lines (48 loc) · 1.71 KB
/
DesignHashMap.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Question Link: https://leetcode.com/problems/design-hashmap/
* Primary idea: Use modulo and array of list / linked list to handle handle function and collision.
* Time Complexity: O(n), Space Complexity: O(n)
*/
class MyHashMap {
let keySpace = 2069
var buckets: [[(Int, Int)]]
/** Initialize your data structure here. */
init() {
buckets = Array(repeating: [(Int, Int)](), count: keySpace)
}
/** value will always be non-negative. */
func put(_ key: Int, _ value: Int) {
var bucket = buckets[key % keySpace]
if let index = find(key, bucket) {
bucket[index].1 = value
} else {
bucket.append((key, value))
}
buckets[key % keySpace] = bucket
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
func get(_ key: Int) -> Int {
let bucket = buckets[key % keySpace]
if let index = find(key, bucket) {
return bucket[index].1
} else {
return -1
}
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
func remove(_ key: Int) {
var bucket = buckets[key % keySpace]
guard let index = find(key, bucket) else {
return
}
bucket.swapAt(index, bucket.count - 1)
bucket.removeLast()
buckets[key % keySpace] = bucket
}
private func find(_ key: Int, _ bucket: [(Int, Int)]) -> Int? {
for (i, pair) in bucket.enumerated() where pair.0 == key {
return i
}
return nil
}
}