forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
92 lines (83 loc) · 2.37 KB
/
Solution.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
class MyCircularQueue {
private Integer[] nums;
private int head;
private int tail;
private int size;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
this.nums = new Integer[k];
this.head = -1;
this.tail = -1;
this.size = 0;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if (isFull()) {
return false;
} else if(this.head == this.tail && this.tail == -1){
this.head++;
this.tail++;
nums[this.tail] = value;
} else {
this.tail = (this.tail + 1) % nums.length;
this.nums[this.tail] = value;
}
this.size++;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if (isEmpty()) {
return false;
} else if (this.head == this.tail) {
this.head = -1;
this.tail = -1;
} else {
this.head = (this.head + 1) % this.nums.length;
}
this.size--;
return true;
}
/** Get the front item from the queue. */
public int Front() {
if (isEmpty()) {
return -1;
} else {
return this.nums[this.head];
}
}
/** Get the last item from the queue. */
public int Rear() {
if (isEmpty()) {
return -1;
} else {
return this.nums[this.tail];
}
}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
if (this.size == 0) {
return true;
} else {
return false;
}
}
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
if (this.size == this.nums.length) {
return true;
} else {
return false;
}
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/