Skip to content

Commit 70de29a

Browse files
committed
solve 641.design-circular-deque
1 parent a77feab commit 70de29a

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* @lc app=leetcode id=641 lang=java
3+
*
4+
* [641] Design Circular Deque
5+
*
6+
* https://leetcode.com/problems/design-circular-deque/description/
7+
*
8+
* algorithms
9+
* Medium (48.68%)
10+
* Likes: 120
11+
* Dislikes: 30
12+
* Total Accepted: 8.5K
13+
* Total Submissions: 17.1K
14+
* Testcase Example: '["MyCircularDeque","insertLast","insertLast","insertFront","insertFront","getRear","isFull","deleteLast","insertFront","getFront"]\n[[3],[1],[2],[3],[4],[],[],[],[4],[]]'
15+
*
16+
* Design your implementation of the circular double-ended queue (deque).
17+
*
18+
* Your implementation should support following operations:
19+
*
20+
*
21+
* MyCircularDeque(k): Constructor, set the size of the deque to be k.
22+
* insertFront(): Adds an item at the front of Deque. Return true if the
23+
* operation is successful.
24+
* insertLast(): Adds an item at the rear of Deque. Return true if the
25+
* operation is successful.
26+
* deleteFront(): Deletes an item from the front of Deque. Return true if the
27+
* operation is successful.
28+
* deleteLast(): Deletes an item from the rear of Deque. Return true if the
29+
* operation is successful.
30+
* getFront(): Gets the front item from the Deque. If the deque is empty,
31+
* return -1.
32+
* getRear(): Gets the last item from Deque. If the deque is empty, return
33+
* -1.
34+
* isEmpty(): Checks whether Deque is empty or not. 
35+
* isFull(): Checks whether Deque is full or not.
36+
*
37+
*
38+
*
39+
*
40+
* Example:
41+
*
42+
*
43+
* MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to
44+
* be 3
45+
* circularDeque.insertLast(1); // return true
46+
* circularDeque.insertLast(2); // return true
47+
* circularDeque.insertFront(3); // return true
48+
* circularDeque.insertFront(4); // return false, the queue is full
49+
* circularDeque.getRear(); // return 2
50+
* circularDeque.isFull(); // return true
51+
* circularDeque.deleteLast(); // return true
52+
* circularDeque.insertFront(4); // return true
53+
* circularDeque.getFront(); // return 4
54+
*
55+
*
56+
*
57+
*
58+
* Note:
59+
*
60+
*
61+
* All values will be in the range of [0, 1000].
62+
* The number of operations will be in the range of [1, 1000].
63+
* Please do not use the built-in Deque library.
64+
*
65+
*
66+
*/
67+
class MyCircularDeque {
68+
69+
private int[] data;
70+
private int head;
71+
private int tail;
72+
private int size;
73+
74+
/** Initialize your data structure here. Set the size of the deque to be k. */
75+
public MyCircularDeque(int k) {
76+
data = new int[k + 1];
77+
size = k + 1;
78+
head = 0;
79+
tail = 0;
80+
}
81+
82+
/** Adds an item at the front of Deque. Return true if the operation is successful. */
83+
public boolean insertFront(int value) {
84+
if (isFull()) {
85+
return false;
86+
}
87+
head = (head - 1 + size) % size;
88+
data[head] = value;
89+
return true;
90+
}
91+
92+
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
93+
public boolean insertLast(int value) {
94+
if (isFull()) {
95+
return false;
96+
}
97+
data[tail] = value;
98+
tail = (tail + 1) % size;
99+
return true;
100+
}
101+
102+
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
103+
public boolean deleteFront() {
104+
if (isEmpty()) {
105+
return false;
106+
}
107+
head = (head + 1) % size;
108+
return true;
109+
}
110+
111+
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
112+
public boolean deleteLast() {
113+
if (isEmpty()) {
114+
return false;
115+
}
116+
tail = (tail - 1 + size) % size;
117+
return true;
118+
}
119+
120+
/** Get the front item from the deque. */
121+
public int getFront() {
122+
if (isEmpty()) {
123+
return -1;
124+
}
125+
return data[head];
126+
}
127+
128+
/** Get the last item from the deque. */
129+
public int getRear() {
130+
if (isEmpty()) {
131+
return -1;
132+
}
133+
return data[(tail - 1 + size) % size];
134+
}
135+
136+
/** Checks whether the circular deque is empty or not. */
137+
public boolean isEmpty() {
138+
return head == tail;
139+
}
140+
141+
/** Checks whether the circular deque is full or not. */
142+
public boolean isFull() {
143+
return (tail + 1) % size == head;
144+
}
145+
}
146+
147+
/**
148+
* Your MyCircularDeque object will be instantiated and called as such:
149+
* MyCircularDeque obj = new MyCircularDeque(k);
150+
* boolean param_1 = obj.insertFront(value);
151+
* boolean param_2 = obj.insertLast(value);
152+
* boolean param_3 = obj.deleteFront();
153+
* boolean param_4 = obj.deleteLast();
154+
* int param_5 = obj.getFront();
155+
* int param_6 = obj.getRear();
156+
* boolean param_7 = obj.isEmpty();
157+
* boolean param_8 = obj.isFull();
158+
*/
159+

0 commit comments

Comments
 (0)