Zack Shorts - Interview Prep
GithubLinkedInLeetCode
  • leetcode
    • 0001 - Two Sum
    • 0002 - Add Two Numbers
    • 0003 - Longest Substring Without Repeating Characters
    • 0004 - Median of Two Sorted Arrays
    • 0005 - Longest Palindromic Substring
    • 0006 - ZigZag Conversion
    • 0007 - Reverse Integer
    • 0009 - Palindrome Number
    • 0011 - Container With Most Water
    • 0013 - Roman to Integer
    • 0014 - Longest Common Prefix
    • 0015 - 3 Sum
    • 0017 - Letter Combinations of a Phone Number
    • 0019 - Remove Nth Node From End of List
    • 0020 - Valid Parentheses
    • 0021 - Merge Two Sorted Lists
    • 0022 - Generate Parentheses
    • 0026 - Remove Duplicates from Sorted Array
    • 0027 - Remove Element
    • 0028 - Implement strStr()
    • 0033 - Searching in Rotated Sorted Array
    • 0034 - Find First and Last Position of Element in Sorted Array
    • 0035 - Search Insert Position
    • 0036 - Valid Sudoku
    • 0039 - Combination Sum
    • 0040 - Combination Sum II
    • 0042 - Multiply Strings
    • 0045 - Jump Game II
    • 0046 - Permutations
    • 0047 - Permutations II
    • 0048 - Rotate Image
    • 0049 - Group Anagrams
    • 0053 - Maximum Subarray
    • 0055 - Jump Game
    • 0056 - Merge Intervals
    • 0058 - Length of Last Word
    • 0059 - Spiral Matrix II
    • 0062 - Unique Paths
    • 0066 - Plus One
    • 0067 Add Binary
    • 0069 - Sqrt(x)
    • 0070 - Climbing Stairs
    • 0072 - Edit Distance
    • 0074 - Search a 2D Matrix
    • 0075 - Sort Colors
    • 0076 - Design HashMap
    • 0077 - Combinations
    • 0078 - Subsets
    • 0079 - Word Search
    • 0082 - Remove Duplicates from Sorted List II
    • 0083 - Remove Duplicates from Sorted List
    • 0088 - Merge Sorted Array
    • 0090 - Subsets II
    • 0091 - Decode Ways
    • 0094 - Binary Tree Inorder Traversal
    • 0098 - Validate Binary Search Tree
    • 0100 - Same Tree
    • 0101 - Symmetric Tree
    • 0102 - Binary Tree Level Order Traversal
    • 0104 - Maximum Depth of Binary Tree
    • 0108 - Convert Sorted Arary to Binary Search Tree
    • 0110 - Balance Binary Tree
    • 0111 - Minimum Depth of Binary Tree
    • 0112 - Path Sum
    • 0116 - Populating Next Right Pointers in Each Node
    • 0117 - Populating Next Right Pointers in Each Node 2
    • 0118 - Pascal's Triangle
    • 0119 - Pascal's Triangle II
    • 0120 - Triangle
    • 0121 - Best Time to Buy and Sell Stock
    • 0130 - Surrounded Regions
    • 0136 - Single Number
    • 0139 - Word Break
    • 0141 - Linked List Cycle
    • 0142 - Linked List Cycle 2
    • 0144 - Binary Tree Preorder Traversal
    • 0145 - Binary Tree Postorder Traversal
    • 0146 - LRU Cache
    • 0149 - Max Points on a Line
    • 0152 - Maximum Product Subarray
    • 0153 - Find Minimum in Rotated Sorted Array
    • 0160 - Intersection of Two Linked Lists
    • 0162 - Find Peak Element
    • 0167 - Two Sum II - Input Array Is Sorted
    • 0169 - Majority Element
    • 0187 - Repeated DNA Subsequences
    • 0189 - Rotate Array
    • 0190 - Reverse Bits
    • 0191 - Number of 1 Bits
    • 0198 - House Robber
    • 0200 - Number of Islands
    • 0201 - Bitwise AND of Numbers Range
    • 0202 - Happy Number
    • 0203 - Remove Linked List Elements
    • 0206 - Reverse Linked List
    • 0209 - Minimum Size Subarray Sum
    • 0213 - House Robber II
    • 0217 - Contains Duplicates
    • 0226 - Invert Binary Tree
    • 0231 - Power of Two
    • 0232 - Implement Queue using Stacks
    • 0235 - Lowest Common Ancestor of a Binary Search Tree
    • 0238 - Product of Sorted Array Except Self
    • 0240 - Search a 2D Matrix II
    • 0242 - Valid Anagram
    • 0278 - First Bad Version
    • 0283 - Move Zeroes
    • 0290 - Word Pattern
    • 0300 - Longest Increasing Subsequence
    • 0322 - Coin Change
    • 0334
    • 0343 - Integer Break
    • 0344 - Reverse String
    • 0350 - Intersection of Two Arrays II
    • 0383 - Ransom Note
    • 0384 - Shuffle an Array
    • 0387 - First Unique Character in a String
    • 0409 - Longest Palindrome
    • 0413 - Arithmetic Slices
    • 0415 - Add String
    • 0435 - Non-overlapping Intervals
    • 0438 - Find All Anagrams in a String
    • 0485 - Max Consecutive Ones
    • 0542 - Zero One Matrix
    • 0547 - Number of Provinces
    • 0557 - Reverse Words in a String III
    • 0560 - Subarray Sum Equals K
    • 0556 - Reshape the Matrix
    • 0567 - Permutation in String
    • 572 - Subtree of Another Tree
    • 0583 - Delete Operation for Two Strings
    • 0617 - Merge Two Binary Trees
    • 0653 - Two Sum IV - Input is a BST
    • 0673 - Number of Longest Increasing Subsequence
    • 0695 - Max Area of Island
    • 0700 - Search in a Binary Search Tree
    • 0701 - Insert into a Binary Search Tree
    • 0704 - Binary Search
    • 0713 - Subarray Product Less Than K
    • 0733 - Flood Fill
    • 0763 - Partition Labels
    • 0784 - Letter Case Permutation
    • 797 - All Paths From Source to Target
    • 0844 - Backspace String Compare
    • 0876 - Middle of the Linked List
    • 0937 - Reorder Data in Log Files
    • 0938 - Range Sum of BST
    • 0941 - Valid Mountain Array
    • 0977 - Squares of a Sorted Array
    • 0986 - Interval List Intersections
    • 0542 - 01 Matrix
    • 1041 - Robot Bounded in Circle
    • 1089 - Duplicate Zeros
    • 1091 - Shortest Path in Binary Matrix
    • 1143 - Longest Common Subsequence
    • 1295 - Find Numbers with Even Number of Digits
    • 1346 - Check if N and its Double Exist
    • 1446 - Consecutive Characters
Powered by GitBook
On this page
  • Examples
  • Java Solution
  1. leetcode

0076 - Design HashMap

Previous0075 - Sort ColorsNext0077 - Combinations

Last updated 3 years ago

Design a HashMap without using any built-in hash table libraries.

Implement the MyHashMap class:

  • MyHashMap() initializes the object with an empty map.

  • void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.

  • int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.

  • void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.

Examples

Input

["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]

[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]

Output

[null, null, null, 1, -1, null, 1, null, -1]

Explanation

MyHashMap myHashMap = new MyHashMap();

myHashMap.put(1, 1); // The map is now [[1,1]]

myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]

myHashMap.get(1); // return 1, The map is now [[1,1], [2,2]]

myHashMap.get(3); // return -1 (i.e., not found), The map is now [[1,1], [2,2]]

myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)

myHashMap.get(2); // return 1, The map is now [[1,1], [2,1]]

myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]

myHashMap.get(2); // return -1 (i.e., not found), The map is now [[1,1]]

Java Solution

class MyHashMap {

    int[] data;
    
    public MyHashMap() {
        data = new int[1000001];
        Arrays.fill(data, -1);
    }
    
    public void put(int key, int value) {
        data[key] = value;
    }
    
    public int get(int key) {
        return data[key];
    }
    
    public void remove(int key) {
        data[key] = -1;
    }
}

/**
 * Your MyHashMap object will be instantiated and called as such:
 * MyHashMap obj = new MyHashMap();
 * obj.put(key,value);
 * int param_2 = obj.get(key);
 * obj.remove(key);
 */
0076 - Design HashMap