题目:设计循环双端队列
- 题号:641
- 难度:中等
- https://leetcode-cn.com/problems/design-circular-deque/
设计实现双端队列。
MyCircularDeque(k)
:构造函数,双端队列的大小为k。insertFront()
:将一个元素添加到双端队列头部。 如果操作成功返回 true。insertLast()
:将一个元素添加到双端队列尾部。如果操作成功返回 true。deleteFront()
:从双端队列头部删除一个元素。 如果操作成功返回 true。deleteLast()
:从双端队列尾部删除一个元素。如果操作成功返回 true。getFront()
:从双端队列头部获得一个元素。如果双端队列为空,返回 -1。getRear()
:获得双端队列的最后一个元素。如果双端队列为空,返回 -1。isEmpty()
:检查双端队列是否为空。isFull()
:检查双端队列是否满了。
示例:
输入
["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 circularDeque = new MycircularDeque(3); // 设置容量大小为3
circularDeque.insertLast(1); // 返回 true
circularDeque.insertLast(2); // 返回 true
circularDeque.insertFront(3); // 返回 true
circularDeque.insertFront(4); // 已经满了,返回 false
circularDeque.getRear(); // 返回 2
circularDeque.isFull(); // 返回 true
circularDeque.deleteLast(); // 返回 true
circularDeque.insertFront(4); // 返回 true
circularDeque.getFront(); // 返回 4
提示:
- 1 <= k <= 1000
- 0 <= value <= 1000
- insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull 调用次数不大于 2000 次
实现
C# 语言
public class MyCircularDeque
{
private int _pFront;
private int _pRear;
private readonly int[] _dataset;
private int _length;
private int _maxSize;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k)
{
_dataset = new int[k];
_length = 0;
_maxSize = k;
_pFront = 0;
_pRear = 0;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public bool InsertFront(int value)
{
if (_length == _maxSize)
return false;
_pFront = (_pFront - 1 + _maxSize)%_maxSize;
_dataset[_pFront] = value;
_length++;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public bool InsertLast(int value)
{
if (_length == _maxSize)
return false;
_dataset[_pRear] = value;
_pRear = (_pRear + 1)%_maxSize;
_length++;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public bool DeleteFront()
{
if (_length == 0)
return false;
_pFront = (_pFront + 1)%_maxSize;
_length--;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public bool DeleteLast()
{
if (_length == 0)
return false;
_pRear = (_pRear - 1 + _maxSize)%_maxSize;
_length--;
return true;
}
/** Get the front item from the deque. */
public int GetFront()
{
if (_length == 0)
return -1;
return _dataset[_pFront];
}
/** Get the last item from the deque. */
public int GetRear()
{
if (_length == 0)
return -1;
int index = (_pRear - 1 + _maxSize)%_maxSize;
return _dataset[index];
}
/** Checks whether the circular deque is empty or not. */
public bool IsEmpty()
{
return _length == 0;
}
/** Checks whether the circular deque is full or not. */
public bool IsFull()
{
return _length == _maxSize;
}
}
/**
* 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();
*/
Python 语言:
class MyCircularDeque(object):
def __init__(self, k):
"""
:type k: int
"""
self._dataset = [0] * k
self._pFront = 0
self._pRear = 0
self._length = 0
self._maxSize = k
def insertFront(self, value):
"""
:type value: int
:rtype: bool
"""
if self._length == self._maxSize:
return False
self._pFront = (self._pFront - 1 + self._maxSize) % self._maxSize
self._dataset[self._pFront] = value
self._length += 1
return True
def insertLast(self, value):
"""
:type value: int
:rtype: bool
"""
if self._length == self._maxSize:
return False
self._dataset[self._pRear] = value
self._pRear = (self._pRear + 1) % self._maxSize
self._length += 1
return True
def deleteFront(self):
"""
:rtype: bool
"""
if self._length == 0:
return False
self._pFront = (self._pFront + 1) % self._maxSize
self._length -= 1
return True
def deleteLast(self):
"""
:rtype: bool
"""
if self._length == 0:
return False
self._pRear = (self._pRear - 1 + self._maxSize) % self._maxSize
self._length -= 1
return True
def getFront(self):
"""
:rtype: int
"""
if self._length == 0:
return -1
return self._dataset[self._pFront]
def getRear(self):
"""
:rtype: int
"""
if self._length == 0:
return -1
index = (self._pRear - 1 + self._maxSize) % self._maxSize
return self._dataset[index]
def isEmpty(self):
"""
:rtype: bool
"""
return self._length == 0
def isFull(self):
"""
:rtype: bool
"""
return self._length == self._maxSize
# Your MyCircularDeque object will be instantiated and called as such:
# obj = MyCircularDeque(k)
# param_1 = obj.insertFront(value)
# param_2 = obj.insertLast(value)
# param_3 = obj.deleteFront()
# param_4 = obj.deleteLast()
# param_5 = obj.getFront()
# param_6 = obj.getRear()
# param_7 = obj.isEmpty()
# param_8 = obj.isFull()