diff --git a/design-hashmap.java b/design-hashmap.java new file mode 100644 index 00000000..a75a3b79 --- /dev/null +++ b/design-hashmap.java @@ -0,0 +1,34 @@ +// Time Complexity :O(1) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :No + +import java.util.Arrays; +class MyHashMap { + + int[] hashstorage = new int[1000001]; + + public MyHashMap() { + Arrays.fill(hashstorage, -1); + } + + public void put(int key, int value) { + hashstorage[key] = value; + } + + public int get(int key) { + return hashstorage[key]; + } + + public void remove(int key) { + hashstorage[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); + */ \ No newline at end of file diff --git a/implement-queue-using-stacks.java b/implement-queue-using-stacks.java new file mode 100644 index 00000000..48e59b7a --- /dev/null +++ b/implement-queue-using-stacks.java @@ -0,0 +1,45 @@ +// Time Complexity : O(1) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this :No + +import java.util.Stack; + +class MyQueue { + Stack in = new Stack<>(); + Stack out = new Stack<>(); + public MyQueue() { + + } + + public void push(int x) { + in.push(x); + } + + public int pop() { + peek(); + return out.pop(); + } + + public int peek() { + if(out.isEmpty()){ + while(!in.isEmpty()){ + out.push(in.pop()); + } + } + return out.peek(); + } + + public boolean empty() { + return out.isEmpty() && in.isEmpty(); + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ \ No newline at end of file