forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_622.java
60 lines (49 loc) · 1.65 KB
/
_622.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
package com.fishercoder.solutions;
import java.util.Arrays;
public class _622 {
public static class Solution1 {
public static class MyCircularQueue {
int[] arr;
int rearIndex;//this one points to the rear of the queue and could grow to 3000 which is the max calls that this problem is bound to
int size;//this is the max size of this circule queue
int frontIndex;
public MyCircularQueue(int k) {
arr = new int[k];
Arrays.fill(arr, -1);
size = k;
rearIndex = 0;
frontIndex = 0;
}
public boolean enQueue(int value) {
if (arr[rearIndex % size] == -1) {
arr[rearIndex % size] = value;
rearIndex++;
return true;
} else {
return false;
}
}
public boolean deQueue() {
if (arr[frontIndex % size] != -1) {
arr[frontIndex % size] = -1;
frontIndex++;
return true;
} else {
return false;
}
}
public int Front() {
return arr[frontIndex % size];
}
public int Rear() {
return arr[(rearIndex - 1) % size];
}
public boolean isEmpty() {
return rearIndex == frontIndex;
}
public boolean isFull() {
return Math.abs(rearIndex - frontIndex) == size;
}
}
}
}