641。設計圓形雙端隊列
難度:中
主題:陣列、鍊錶、設計、隊列
設計循環雙端隊列(deque)的實作。
實作 MyCircularDeque 類別:
範例1:
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"] [[3], [1], [2], [3], [4], [], [], [], [4], []]
[null, true, true, true, false, 2, true, true, true, 4]
MyCircularDeque myCircularDeque = new MyCircularDeque(3); myCircularDeque.insertLast(1); // return True myCircularDeque.insertLast(2); // return True myCircularDeque.insertFront(3); // return True myCircularDeque.insertFront(4); // return False, the queue is full. myCircularDeque.getRear(); // return 2 myCircularDeque.isFull(); // return True myCircularDeque.deleteLast(); // return True myCircularDeque.insertFront(4); // return True myCircularDeque.getFront(); // return 4
約束:
解:
我們可以使用陣列來表示雙端隊列結構。我們將維護一個頭指針和尾指針,幫助我們追蹤雙端隊列的前部和後部,並在必要時環繞以實現循環行為。
讓我們用 PHP 實作這個解:641。設計圓形雙端隊列
這是 MyCircularDeque 類別的逐步解決方案:
<?php class MyCircularDeque { /** * @var array */ private $deque; /** * @var int */ private $maxSize; /** * @var int */ private $front; /** * @var int */ private $rear; /** * @var int */ private $size; /** * Initialize your data structure here. Set the size of the deque to be k. * * @param Integer $k */ function __construct($k) { ... ... ... /** * go to ./solution.php */ } /** * Adds an item at the front of Deque. Return true if the operation is successful. * * @param Integer $value * @return Boolean */ function insertFront($value) { ... ... ... /** * go to ./solution.php */ } /** * Adds an item at the rear of Deque. Return true if the operation is successful. * * @param Integer $value * @return Boolean */ function insertLast($value) { ... ... ... /** * go to ./solution.php */ } /** * Deletes an item from the front of Deque. Return true if the operation is successful. * * @return Boolean */ function deleteFront() { ... ... ... /** * go to ./solution.php */ } /** * Deletes an item from the rear of Deque. Return true if the operation is successful. * * @return Boolean */ function deleteLast() { ... ... ... /** * go to ./solution.php */ } /** * Get the front item from the deque. If the deque is empty, return -1. * * @return Integer */ function getFront() { ... ... ... /** * go to ./solution.php */ } /** * Get the last item from the deque. If the deque is empty, return -1. * * @return Integer */ function getRear() { ... ... ... /** * go to ./solution.php */ } /** * Checks whether the deque is empty or not. * * @return Boolean */ function isEmpty() { ... ... ... /** * go to ./solution.php */ } /** * Checks whether the deque is full or not. * * @return Boolean */ function isFull() { ... ... ... /** * go to ./solution.php */ } } /** * Your MyCircularDeque object will be instantiated and called as such: * $obj = MyCircularDeque($k); * $ret_1 = $obj->insertFront($value); * $ret_2 = $obj->insertLast($value); * $ret_3 = $obj->deleteFront(); * $ret_4 = $obj->deleteLast(); * $ret_5 = $obj->getFront(); * $ret_6 = $obj->getRear(); * $ret_7 = $obj->isEmpty(); * $ret_8 = $obj->isFull(); */ ?>
初始化:
insertFront($value):
insertLast($value):
deleteFront():
deleteLast():
getFront():
getRear():
isEmpty():
isFull():
$myCircularDeque = new MyCircularDeque(3); // Initialize deque with size 3 $myCircularDeque->insertLast(1); // return true $myCircularDeque->insertLast(2); // return true $myCircularDeque->insertFront(3); // return true $myCircularDeque->insertFront(4); // return false, deque is full echo $myCircularDeque->getRear(); // return 2 echo $myCircularDeque->isFull(); // return true $myCircularDeque->deleteLast(); // return true $myCircularDeque->insertFront(4); // return true echo $myCircularDeque->getFront(); // return 4
聯絡連結
如果您發現本系列有幫助,請考慮在 GitHub 上給 存儲庫 一個星號或在您最喜歡的社交網絡上分享該帖子? 。您的支持對我來說意義重大!
如果您想要更多類似的有用內容,請隨時關注我:
以上是。設計循環雙端佇列的詳細內容。更多資訊請關注PHP中文網其他相關文章!