Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions problems/design-hashmap/design_hashmap.go
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
package design_hashmap

type MyHashMap struct {
data map[int]int
}

/** Initialize your data structure here. */
func Constructor() MyHashMap {
return MyHashMap{make(map[int]int, 0)}
}

/** value will always be non-negative. */
func (this *MyHashMap) Put(key int, value int) {
this.data[key] = value
}

/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
func (this *MyHashMap) Get(key int) int {
if v, ok := this.data[key]; ok {
return v
}
return -1
}

/** Removes the mapping of the specified value key if this map contains a mapping for the key */
func (this *MyHashMap) Remove(key int) {
delete(this.data, key)
}

/**
* Your MyHashMap object will be instantiated and called as such:
* obj := Constructor();
* obj.Put(key,value);
* param_2 := obj.Get(key);
* obj.Remove(key);
*/
17 changes: 17 additions & 0 deletions problems/design-hashmap/design_hashmap_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
package design_hashmap

import "testing"

func TestDesignHashmap(t *testing.T) {
obj := Constructor()
obj.Put(1, 1)
obj.Put(2, 2)
output := obj.Get(1) == 1
output = obj.Get(3) == -1 && output
obj.Put(2, 1)
output = obj.Get(2) == 1 && output
obj.Remove(2)
output = obj.Get(2) == -1 && output
if !output {
t.Fatalf("output: %v, expected: %v", output, true)
}
}