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中文网其他相关文章!