LeetCode: Design Hashmap Solution

1/**
2 * Initialize your data structure here.
3 */
4var MyHashMap = function () {
5 this.obj = {}
6}
7
8/**
9 * value will always be non-negative.
10 * @param {number} key
11 * @param {number} value
12 * @return {void}
13 */
14MyHashMap.prototype.put = function (key, value) {
15 this.obj[key] = value
16}
17
18/**
19 * Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
20 * @param {number} key
21 * @return {number}
22 */
23MyHashMap.prototype.get = function (key) {
24 return this.obj.hasOwnProperty(key) ? this.obj[key] : -1
25}
26
27/**
28 * Removes the mapping of the specified value key if this map contains a mapping for the key
29 * @param {number} key
30 * @return {void}
31 */
32MyHashMap.prototype.remove = function (key) {
33 delete this.obj[key]
34}
35
36/**
37 * Your MyHashMap object will be instantiated and called as such:
38 * var obj = new MyHashMap()
39 * obj.put(key,value)
40 * var param_2 = obj.get(key)
41 * obj.remove(key)
42 */

Comments

Loading comments...

Tags

leetcode

hash table

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Remove Palindromic Subsequences

Mar 9, 2021

HoningJS

Search Posts