LeetCode: Design Hashmap Solution
1/**2 * Initialize your data structure here.3 */4var MyHashMap = function () {5 this.obj = {}6}78/**9 * value will always be non-negative.10 * @param {number} key11 * @param {number} value12 * @return {void}13 */14MyHashMap.prototype.put = function (key, value) {15 this.obj[key] = value16}1718/**19 * Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key20 * @param {number} key21 * @return {number}22 */23MyHashMap.prototype.get = function (key) {24 return this.obj.hasOwnProperty(key) ? this.obj[key] : -125}2627/**28 * Removes the mapping of the specified value key if this map contains a mapping for the key29 * @param {number} key30 * @return {void}31 */32MyHashMap.prototype.remove = function (key) {33 delete this.obj[key]34}3536/**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.