LeetCode 641. 设计循环双端队列
原题链接
中等
作者:
ericf
,
2022-08-15 13:00:20
,
所有人可见
,
阅读 172
class MyCircularDeque {
public:
int t;
MyCircularDeque(int k) {
t=k;
}
int s[3010];
int hh=1500,tt=1499;
bool insertFront(int value) {
if(tt-hh+1>=t) return false;
else{
hh--;
s[hh] = value;
return true;
}
}
bool insertLast(int value) {
if(tt-hh+1>=t) return false;
else{
s[++tt] = value;
return true;
}
}
bool deleteFront() {
if(tt-hh+1>=1){
hh+=1;
return true;
}
else return false;
}
bool deleteLast() {
if(tt-hh+1>=1){
tt-=1;
return true;
}
else return false;
}
int getFront() {
if(hh<=tt)
{
return s[hh];
}
return -1;
}
int getRear() {
if(hh>tt) return -1;
else return s[tt];
}
bool isEmpty() {
if(hh>tt) return true;
else return false;
}
bool isFull() {
if(tt-hh+1>=t) return true;
else return false;
}
};
/**
* 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();
*/