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

ImplementQueueUsingStackCompleted_HashSetPending #39

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
114 changes: 114 additions & 0 deletions HashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Time Complexity :
//mthods::add:O(n)
//remove:O(n)
//contains:O(n)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :I was getting null pointer exception in contains method and type incompatible exception while assigning values in wrong manner
//this is new commeng-------------------

// Your code here along with comments explaining your approach

class MyHashSet {
//an array node of the objexts of class ListNode was created
ListNode[] node=new ListNode[1000000];

/** Initialize your data structure here. */
private class ListNode
{ //ListNode class contains attributes key and next
int key;
ListNode next;
//key was initialised in the constructor
ListNode(int key)
{
this.key=key;
}

}
public MyHashSet()
{

}
//to find index of the given key we are using hashCode as follows
int index(int key)
{
return Integer.hashCode(key)%node.length;
}
//a method is made to find the location previous than the node to be found in parameters
ListNode find(ListNode head,int key)
{ //curr pointer is assigned on the starting of the array and prev is null
ListNode curr=head;
ListNode prev=null;
//while curr is not null and curr key is equal to the required value,loop is continued
while(curr!=null && curr.key!=key)
{//curr is assigned to prev
prev=curr;
//curr is incremented
curr=curr.next;
}
//at end of the loop,it gives node previous to the key to be found
return prev;
}

public void add(int key)
{ //index of the key is stored in integer i
int i=index(key);
//if the node is null,we'll create a new node having key=-1
if(node[i]==null)
{
node[i]=new ListNode(-1);
}
//pointer previous to the node is found
ListNode prev=find(node[i],key);
//if next to the prev is null,A new listnode is inserted at the end
if(prev.next==null)
{
prev.next=new ListNode(key);
}
else
{ //key is updated to its value
prev.next.key=key;
}
}


public void remove(int key)
{ //index is found
int i=index(key);
//if node is not there, do nothing
if(node[i]==null)
{return;}
//prev to the node is found
ListNode prev=find(node[i],key);

//if the node is null, do nothing
if(prev.next==null){return;}
//we point the pointer to next to the node to be removed
prev.next=prev.next.next;
}

/** Returns true if this set contains the specified element */
public boolean contains(int key)
{
//index is found
int i=index(key);
//if it is not there return false
if(node[i]==null)
{return false;}
//previous to the node is found
ListNode prev=find(node[i],key);
//if next to the node is not null,check for the value and if it is there,return true else false
if( prev.next!=null)
{ if(prev.next.key==key )
{return true;}}
return false;
}
}

/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/
73 changes: 73 additions & 0 deletions HashSetUsingDoubleHashing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Time Complexity :1
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :I was getting index out of bound exception and null pointer exception also
//this is new commeng-------------------

// Your code here along with comments explaining your approach
//
class MyHashSet {

//initialising an integer variable buckets to 1000
//initialising an integer variable bucketItems to 1000
int buckets=1000;
int bucketItems=1000;
//making a two dimensional array if buckets and bucketItems
boolean[][] storage=new boolean[buckets][];
//method to return the value of bucket
private int bucket(int value)
{//return position of the value in main array
return value%buckets;
}
//method to return the value of bucketItem
private int bucketItem(int value)
{ //return position of the value in nested array
return value/buckets;
}
/** Initialize your data structure here. */
public MyHashSet() {

}

public void add(int value)
{ //get the positions of the value element
int bucket=bucket(value);
int bucketItem=bucketItem(value);
//if yhe main list is null, make a new element in the array
if(storage[bucket]==null)
{storage[bucket]=new boolean[bucketItems];
}
//give the value in nested array as true
storage[bucket][bucketItem]=true;
}

public void remove(int value)
{ //get the positions of the value element
int bucket=bucket(value);
int bucketItem=bucketItem(value);
//if the position is not empty
if(storage[bucket]!=null)
//give the complete position(nested array) as false
{storage[bucket][bucketItem]=false;
}

}

/** Returns true if this set contains the specified element */
public boolean contains(int value)
{ //get the positions of the value element
int bucket=bucket(value);
int bucketItem=bucketItem(value);
//if the position is not empty return true either false
return storage[bucket]!=null && storage[bucket][bucketItem];

}
}

/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/
62 changes: 62 additions & 0 deletions QueueUsingStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Time Complexity :1
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :No
//this is new commeng-------------------

// Your code here along with comments explaining your approach

class MyQueue {
//creating two sttacks to keep track of minimum w.r.t the respective element
Stack<Integer> s1=new Stack<>();
Stack<Integer> s2=new Stack<>();

/** Initialize your data structure here. */
public MyQueue() {

}

/** Push element x to the back of queue. */
public void push(int x) {
//simply push the element in s1
s1.push(x);
}

/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(s2.isEmpty())
{ //if s1 is not empty,push all the elements of s1 in s2
while(!s1.isEmpty())
{s2.push(s1.pop());}
}
//return and pop the top of the s2 stack
return s2.pop();
}

/** Get the front element. */
public int peek() {
if(s2.isEmpty())
{ //if s1 is not empty,push all the elements of s1 in s2
while(!s1.isEmpty())
{s2.push(s1.pop());}
}
//return top of the s2 stack
return s2.peek();
}

/** Returns whether the queue is empty. */
public boolean empty() {
//if both the stacks are empty return true
return (s1.isEmpty() && s2.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();
*/