641. Design Circular Deque Medium

@problem@discussion
#Array#Linked List#Design#Queue



1/**
2 * [641] Design Circular Deque
3 *
4 * Design your implementation of the circular double-ended queue (deque).
5 * Implement the MyCircularDeque class:
6 * 
7 * 	MyCircularDeque(int k) Initializes the deque with a maximum size of k.
8 * 	boolean insertFront() Adds an item at the front of Deque. Returns true if the operation is successful, or false otherwise.
9 * 	boolean insertLast() Adds an item at the rear of Deque. Returns true if the operation is successful, or false otherwise.
10 * 	boolean deleteFront() Deletes an item from the front of Deque. Returns true if the operation is successful, or false otherwise.
11 * 	boolean deleteLast() Deletes an item from the rear of Deque. Returns true if the operation is successful, or false otherwise.
12 * 	int getFront() Returns the front item from the Deque. Returns -1 if the deque is empty.
13 * 	int getRear() Returns the last item from Deque. Returns -1 if the deque is empty.
14 * 	boolean isEmpty() Returns true if the deque is empty, or false otherwise.
15 * 	boolean isFull() Returns true if the deque is full, or false otherwise.
16 * 
17 *  
18 * Example 1:
19 * 
20 * Input
21 * ["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
22 * [[3], [1], [2], [3], [4], [], [], [], [4], []]
23 * Output
24 * [null, true, true, true, false, 2, true, true, true, 4]
25 * Explanation
26 * MyCircularDeque myCircularDeque = new MyCircularDeque(3);
27 * myCircularDeque.insertLast(1);  // return True
28 * myCircularDeque.insertLast(2);  // return True
29 * myCircularDeque.insertFront(3); // return True
30 * myCircularDeque.insertFront(4); // return False, the queue is full.
31 * myCircularDeque.getRear();      // return 2
32 * myCircularDeque.isFull();       // return True
33 * myCircularDeque.deleteLast();   // return True
34 * myCircularDeque.insertFront(4); // return True
35 * myCircularDeque.getFront();     // return 4
36 * 
37 *  
38 * Constraints:
39 * 
40 * 	1 <= k <= 1000
41 * 	0 <= value <= 1000
42 * 	At most 2000 calls will be made to insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull.
43 * 
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/design-circular-deque/
48// discuss: https://leetcode.com/problems/design-circular-deque/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52struct MyCircularDeque {
53        false
54    }
55
56
57/** 
58 * `&self` means the method takes an immutable reference.
59 * If you need a mutable reference, change it to `&mut self` instead.
60 */
61impl MyCircularDeque {
62
63    fn new(k: i32) -> Self {
64        
65    }
66    
67    fn insert_front(&self, value: i32) -> bool {
68        
69    }
70    
71    fn insert_last(&self, value: i32) -> bool {
72        
73    }
74    
75    fn delete_front(&self) -> bool {
76        
77    }
78    
79    fn delete_last(&self) -> bool {
80        
81    }
82    
83    fn get_front(&self) -> i32 {
84        
85    }
86    
87    fn get_rear(&self) -> i32 {
88        
89    }
90    
91    fn is_empty(&self) -> bool {
92        
93    }
94    
95    fn is_full(&self) -> bool {
96        
97    }
98}
99
100/**
101 * Your MyCircularDeque object will be instantiated and called as such:
102 * let obj = MyCircularDeque::new(k);
103 * let ret_1: bool = obj.insert_front(value);
104 * let ret_2: bool = obj.insert_last(value);
105 * let ret_3: bool = obj.delete_front();
106 * let ret_4: bool = obj.delete_last();
107 * let ret_5: i32 = obj.get_front();
108 * let ret_6: i32 = obj.get_rear();
109 * let ret_7: bool = obj.is_empty();
110 * let ret_8: bool = obj.is_full();
111 */
112
113// submission codes end
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn test_641() {
121    }
122}
123


Back
© 2025 bowen.ge All Rights Reserved.