[LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque).
Your implementation should support following operations:
MyCircularDeque(k): Constructor, set the size of the deque to be k.
insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.
insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.
deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.
deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.
getFront(): Gets the front item from the Deque. If the deque is empty, return -1.
getRear(): Gets the last item from Deque. If the deque is empty, return -1.
isEmpty(): Checks whether Deque is empty or not.
isFull(): Checks whether Deque is full or not.
Example:
MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3
circularDeque.insertLast(1); // return true
circularDeque.insertLast(2); // return true
circularDeque.insertFront(3); // return true
circularDeque.insertFront(4); // return false, the queue is full
circularDeque.getRear(); // return 2
circularDeque.isFull(); // return true
circularDeque.deleteLast(); // return true
circularDeque.insertFront(4); // return true
circularDeque.getFront(); // return 4
Note:
All values will be in the range of [0, 1000].
The number of operations will be in the range of [1, 1000].
Please do not use the built-in Deque library.
622.Design Circular Queue 的拓展。
解法:doubly Linked List
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 | class MyCircularDeque { int size; int k; DoubleListNode head; DoubleListNode tail; /** Initialize your data structure here. Set the size of the deque to be k. */ public MyCircularDeque( int k) { head = new DoubleListNode(- 1 ); tail = new DoubleListNode(- 1 ); head.pre = tail; tail.next = head; this .k = k; this .size = 0 ; } /** Adds an item at the front of Deque. Return true if the operation is successful. */ public boolean insertFront( int value) { if (size == k) return false ; DoubleListNode node = new DoubleListNode(value); node.next = head; node.pre = head.pre; head.pre.next = node; head.pre = node; size++; return true ; } /** Adds an item at the rear of Deque. Return true if the operation is successful. */ public boolean insertLast( int value) { if (size == k) return false ; DoubleListNode node = new DoubleListNode(value); node.next = tail.next; tail.next.pre = node; tail.next = node; node.pre = tail; size++; return true ; } /** Deletes an item from the front of Deque. Return true if the operation is successful. */ public boolean deleteFront() { if (size == 0 ) return false ; head.pre.pre.next = head; head.pre = head.pre.pre; size--; return true ; } /** Deletes an item from the rear of Deque. Return true if the operation is successful. */ public boolean deleteLast() { if (size == 0 ) return false ; tail.next.next.pre = tail; tail.next = tail.next.next; size--; return true ; } /** Get the front item from the deque. */ public int getFront() { return head.pre.val; } /** Get the last item from the deque. */ public int getRear() { return tail.next.val; } /** Checks whether the circular deque is empty or not. */ public boolean isEmpty() { return size == 0 ; } /** Checks whether the circular deque is full or not. */ public boolean isFull() { return size == k; } } class DoubleListNode { DoubleListNode pre; DoubleListNode next; int val; public DoubleListNode( int val) { this .val = val; } } |
Python:
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 | class Node: def __init__( self , value): self .val = value self . next = self .pre = None class MyCircularDeque: def __init__( self , k): self .head = self .tail = Node( - 1 ) self .head. next = self .tail self .tail.pre = self .head self .size = k self .curSize = 0 def add( self , value, preNode): new = Node(value) new.pre = preNode new. next = preNode. next new.pre. next = new. next .pre = new self .curSize + = 1 def remove( self , preNode): node = preNode. next node.pre. next = node. next node. next .pre = node.pre self .curSize - = 1 def insertFront( self , value): if self .curSize < self .size: self .add(value, self .head) return True return False def insertLast( self , value): if self .curSize < self .size: self .add(value, self .tail.pre) return True return False def deleteFront( self ): if self .curSize: self .remove( self .head) return True return False def deleteLast( self ): if self .curSize: self .remove( self .tail.pre.pre) return True return False def getFront( self ): if self .curSize: return self .head. next .val return - 1 def getRear( self ): if self .curSize: return self .tail.pre.val return - 1 def isEmpty( self ): return self .curSize = = 0 def isFull( self ): return self .curSize = = self .size |
C++:
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 | #include <vector> #include <iostream> using namespace std; class MyCircularDeque { private : vector< int > buffer; int cnt; int k; int front; int rear; public : /** Initialize your data structure here. Set the size of the deque to be k. */ MyCircularDeque( int k): buffer(k, 0), cnt(0), k(k), front(k - 1), rear(0) { } /** Adds an item at the front of Deque. Return true if the operation is successful. */ bool insertFront( int value) { if (cnt == k) { return false ; } buffer[front] = value; front = (front - 1 + k) % k; ++cnt; return true ; } /** Adds an item at the rear of Deque. Return true if the operation is successful. */ bool insertLast( int value) { if (cnt == k) { return false ; } buffer[rear] = value; rear = (rear + 1) % k; ++cnt; return true ; } /** Deletes an item from the front of Deque. Return true if the operation is successful. */ bool deleteFront() { if (cnt == 0) { return false ; } front = (front + 1) % k; --cnt; return true ; } /** Deletes an item from the rear of Deque. Return true if the operation is successful. */ bool deleteLast() { if (cnt == 0) { return false ; } rear = (rear - 1 + k) % k; --cnt; return true ; } /** Get the front item from the deque. */ int getFront() { if (cnt == 0) { return -1; } return buffer[(front + 1) % k]; } /** Get the last item from the deque. */ int getRear() { if (cnt == 0) { return -1; } return buffer[(rear - 1 + k) % k]; } /** Checks whether the circular deque is empty or not. */ bool isEmpty() { return cnt == 0; } /** Checks whether the circular deque is full or not. */ bool isFull() { return cnt == k; } }; /** * Your MyCircularDeque object will be instantiated and called as such: * MyCircularDeque obj = new MyCircularDeque(k); * bool param_1 = obj.insertFront(value); * bool param_2 = obj.insertLast(value); * bool param_3 = obj.deleteFront(); * bool param_4 = obj.deleteLast(); * int param_5 = obj.getFront(); * int param_6 = obj.getRear(); * bool param_7 = obj.isEmpty(); * bool param_8 = obj.isFull(); */ #if DEBUG int main( int argc, char ** argv) { return 0; } #endif |
类似题目:
[LeetCode] 622.Design Circular Queue 设计环形队列
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架