-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path836.rectangle-overlap.java
More file actions
59 lines (52 loc) · 1.32 KB
/
Copy path836.rectangle-overlap.java
File metadata and controls
59 lines (52 loc) · 1.32 KB
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
class MyCircularQueue {
/** Initialize your data structure here. Set the size of the queue to be k. */
int[] queue;
int start;
int end;
int k;
public MyCircularQueue(int k) {
this.k = k;
queue = new int[k];
start = 0;
end = 0;
Arrays.fill(queue, -1);
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if (end == start && queue[end] != -1) {
return false;
}
queue[end++] = value;
if (end == k) {
end = 0;
}
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if (queue[start] == -1) {
return false;
}
queue[start++] = -1;
if (start == k) {
start = 0;
}
return true;
}
/** Get the front item from the queue. */
public int Front() {
return queue[start];
}
/** Get the last item from the queue. */
public int Rear() {
return end == 0 ? queue[k - 1] : queue[end - 1];
}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
return start == end && queue[end] == -1;
}
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
return start == end && queue[end] != -1;
}
}