Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design-2 #2042

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
78 changes: 78 additions & 0 deletions HashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
class MyHashMap {

class Node {
int key;
int value;
Node next;
public Node(int key, int value){
this.key=key;
this.value=value;
}
}
Node[] s;
int bucket=10000;
public MyHashMap() {

this.bucket=10000;
this.s=new Node[bucket];
}
int getHash(int key){
return key % bucket;
}
Node getPrev(Node head, int key){
Node prev=null;
Node curr=head;
if(curr!=null && curr.key!=key){
prev=curr;
curr=curr.next;
}
return prev;
}
public void put(int key, int value) {
int idx=getHash(key);
if(s[idx]==null)
s[idx]=new Node(-1,-1);
Node prev=getPrev(s[idx],key);
if(prev.next==null)
prev.next= new Node(key,value);
else{
prev.next.value=value;
}
}

public int get(int key) {
int idx=getHash(key);
if(s[idx]==null){
return -1;
}
Node prev=getPrev(s[idx],key);
if(prev.next==null){
return -1;
}

return prev.next.value;
}

public void remove(int key) {
int idx=getHash(key);
if(s[idx]==null){
return;
}
Node prev=getPrev(s[idx],key);
if(prev.next==null){
return;
}
Node cur=prev.next;
prev.next=cur.next;
cur=null;

}
}

/**
* 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);
*/
46 changes: 46 additions & 0 deletions QueueAsStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class MyQueue {
Stack<Integer> in;
Stack<Integer> out;
public MyQueue() {
this.in= new Stack<>();
this.out= new Stack<>();
}

public void push(int x) {
in.push(x);
}

public int pop() {
if(out.isEmpty()){
while(!in.isEmpty()){
out.push(in.pop());
}
}
return out.pop();
}

public int peek() {
if(out.isEmpty()){
while(!in.isEmpty()){
out.push(in.pop());
}
}
return out.peek();
}

public boolean empty() {
if(in.isEmpty() && out.isEmpty())
return true;
else
return false;
}
}

/**
* 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();
*/