-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue641.java
More file actions
240 lines (215 loc) · 7.27 KB
/
Queue641.java
File metadata and controls
240 lines (215 loc) · 7.27 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package queue;
import java.util.Stack;
/**
* @ProjectName: leetcode
* @Package: PACKAGE_NAME
* @ClassName: Queue641
* @Author: markey
* @Description:
* @Date: 2019/10/6 11:23
* @Version: 1.0
*/
public class Queue641 {
public static void main(String[] args) {
MyCircularDeque2 circularDeque = new MyCircularDeque2(3); // 设置容量大小为3
System.out.println(circularDeque.insertLast(1));// 返回 true
System.out.println(circularDeque.insertLast(2));// 返回 true
System.out.println(circularDeque.insertFront(3));// 返回 true
System.out.println(circularDeque.insertFront(4));// 已经满了,返回 false
System.out.println(circularDeque.getRear());// 返回 2
System.out.println(circularDeque.isFull());// 返回 true
System.out.println(circularDeque.deleteLast());// 返回 true
System.out.println(circularDeque.insertFront(4));// 返回 true
System.out.println(circularDeque.getFront());// 返回 4
}
/**
* 使用循环数组实现,
* 记录队列的头和尾指针
* 执行用时 :64 ms, 在所有 Java 提交中击败了92.38%的用户
* 内存消耗 :38.6 MB, 在所有 Java 提交中击败了98.31%的用户
*/
static class MyCircularDeque2 {
int[] array;
int head, tail, size, count;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque2(int k) {
array = new int[k];
head = 0;
tail = 0;
size = k;
count = 0;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if (isFull()) {
return false;
}
head = (head + size - 1) % size;
array[head] = value;
count ++;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if (isFull()) {
return false;
}
array[tail] = value;
tail = (tail + 1) % size;
count ++;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if (this.isEmpty()) {
return false;
}
head++;
if (head >= size) head -= size;
count --;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if (this.isEmpty()) {
return false;
}
tail--;
if (tail < 0) tail += size;
count --;
return true;
}
/** Get the front item from the deque. */
public int getFront() {
if (this.isEmpty()) {
return -1;
}
return array[head];
}
/** Get the last item from the deque. */
public int getRear() {
if (this.isEmpty()) {
return -1;
}
if (tail - 1 < 0) {
return array[tail + size - 1];
}
return array[tail - 1];
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return count == 0;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return count == size;
}
}
/**
* 使用双栈实现双端队列
* 一个栈的栈顶是头部元素,另一个栈的栈顶是尾部元素
* 执行用时 :63 ms, 在所有 Java 提交中击败了93.81%的用户
* 内存消耗 :37.6 MB, 在所有 Java 提交中击败了98.31%的用户
*/
static class MyCircularDeque {
Stack<Integer> head;
Stack<Integer> tail;
int size;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
head = new Stack<>();
tail = new Stack<>();
size = k;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if (head.size() + tail.size() < size) {
head.push(value);
return true;
}
return false;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if (head.size() + tail.size() < size) {
tail.push(value);
return true;
}
return false;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if (head.isEmpty()) {
while (!tail.isEmpty()) {
head.push(tail.pop());
}
}
if (head.isEmpty()) {
return false;
} else {
head.pop();
return true;
}
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if (tail.isEmpty()) {
while (!head.isEmpty()) {
tail.push(head.pop());
}
}
if (tail.isEmpty()) {
return false;
} else {
tail.pop();
return true;
}
}
/** Get the front item from the deque. */
public int getFront() {
if (head.isEmpty()) {
while (!tail.isEmpty()) {
head.push(tail.pop());
}
}
if (head.isEmpty()) {
return -1;
} else {
return head.peek();
}
}
/** Get the last item from the deque. */
public int getRear() {
if (tail.isEmpty()) {
while (!head.isEmpty()) {
tail.push(head.pop());
}
}
if (tail.isEmpty()) {
return -1;
} else {
return tail.peek();
}
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return head.isEmpty() && tail.isEmpty();
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return head.size() + tail.size() >= size;
}
}
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque obj = new MyCircularDeque(k);
* boolean param_1 = obj.insertFront(value);
* boolean param_2 = obj.insertLast(value);
* boolean param_3 = obj.deleteFront();
* boolean param_4 = obj.deleteLast();
* int param_5 = obj.getFront();
* int param_6 = obj.getRear();
* boolean param_7 = obj.isEmpty();
* boolean param_8 = obj.isFull();
*/
}