forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample.java
110 lines (78 loc) · 2.88 KB
/
Sample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Time Complexity : O(n) where the N is the number of elements in the array
// Space Complexity :O(N) where the N is the number of elements in the array
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :No
// Your code here along with comments explaining your approach
//we defined 2 hashfunctions one on basis of modulo and other on basis of /
class MyHashSet {
/** Initialize your data structure here. */
int bucketsize = 1000;
int bucketitemsize = 1000;
boolean hashset [][];
public MyHashSet() {
hashset = new boolean[1000][];
}
public int bucketindex(int key){
return (key%bucketsize);
}
public int bucketitemindex(int key){
return (key/bucketitemsize);
}
// Inserting element into 2 dimensional array on the basis of hash functions created before
//If it is the first element that has no array to store elements,we create array and then on
//basis of bucketitemindex hash function and then initialize to true else initialize by
//going to the arrayelemnt on hashfunction2 and initializing to true
public void add(int key) {
int bi = bucketindex(key);
int bIi = bucketitemindex(key);
if(hashset[bi]==null){
hashset[bi]= new boolean[bucketitemsize];
}
hashset[bi][bIi]=true;
}
//If the ar[firsthash function] is not null then we calculate arr[hash1fun][hash2fun] to false
public void remove(int key) {
int bi = bucketindex(key);
int bIi = bucketitemindex(key);
if(hashset[bi]!=null)
hashset[bi][bIi]=false;
}
/** Returns true if this set contains the specified element else false, The element that is
//not in the array fails on second condition, if no elements are present for first hashfun
//throws false
public boolean contains(int key) {
int bi = bucketindex(key);
int bIi = bucketitemindex(key);
return ((hashset[bi]!=null) && hashset[bi][bIi]);
}
}
// Time Complexity : o(1)
// Space Complexity :O(N) where the N is the number of elements in the array
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :No
class MyQueue {
private Stack<Integer> input = new Stack<>();
private Stack<Integer> output = new Stack<>();
int front;
public void push(int x) {
input.push(x);
}
public void shift(){
if (output.isEmpty())
{
while (!input.isEmpty())
output.push(input.pop());
}
}
public void pop() {
shift();
output.pop();
}
public int peek() {
shift();
return (output.peek());
}
public boolean empty() {
return input.empty() && output.empty();
}
}