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:

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:

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++:  

#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 设计环形队列

All LeetCode Questions List 题目汇总

[LeetCode] 641.Design Circular Deque 设计环形双向队列的更多相关文章

  1. [LeetCode] Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  2. [LeetCode] 622.Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  3. LeetCode 641. Design Circular Deque

    原题链接在这里:https://leetcode.com/problems/design-circular-deque/ 题目: Design your implementation of the c ...

  4. [LeetCode] Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  5. LC 641. Design Circular Deque

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  6. 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...

  7. Leetcode641.Design Circular Deque设计循环双端队列

    设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront():将一个元素添加到双端队列头部. 如果操作成功返回 tr ...

  8. LeetCode 622. Design Circular Queue

    原题链接在这里:https://leetcode.com/problems/design-circular-queue/ 题目: Design your implementation of the c ...

  9. [Swift]LeetCode641. 设计循环双端队列 | Design Circular Deque

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

随机推荐

  1. adb命令 判断锁屏

    通过adb命令获取手机是否锁屏状态,可以通过下面指令:1.adb shell dumpsys window policy |find "isStatusBarKeyguard"2. ...

  2. Beta 冲刺随笔汇总

    作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Beta冲刺(团队) 团队名称 基于云的胜利冲锋队 作业目标 汇总随笔 团队信息 团队名称:基于云的胜利冲锋队 ...

  3. python预课02 time模块,文本进度条示例,数字类型操作,字符串操作

    time模块 概述:time库是Python中处理时间的标准库,包含以下三类函数 时间获取: time(), ctime(), gmtime() 时间格式化: strftime(), strptime ...

  4. 简单双向链表的实现&新约瑟夫问题

    题目描述: 给定m个人,从s开始报数,数字顺加,报到n的人出列,然后数字顺减报到k的人出列,求出列顺序 样例输入: 8 1 3 2 样例输出: 3 6 1 5 2 8 4 7 分析: 约瑟夫问题主要就 ...

  5. 论文编写工具使用(1)latex软件

    1什么是LaTeX 能用编写程序的模式写论文,将你从格式编辑解脱出来,套用现成的论文程序模板,直接生成.LaTEX(/ˈlɑːtɛx/,常被读作/ˈlɑːtɛk/或/ˈleɪtɛk/),文字形式写作L ...

  6. (10)树莓派 vim编辑器使用

    进去后 1点击 insert 插入数据 2 ctrl+alt+c  粘贴内容 或者 手动敲入代码 3 ctrl+v     保存 4 :wq  保存退出 5 回车

  7. centos7.2(一)vultr服务器购买和开通端口

    https://vultr.me/52.html 之前我们已经介绍了如何购买 Vultr 以及如何使用支付宝对 Vultr 进行充值,相关教程: VULTR 购买教程 2018 年最新图文版 VULT ...

  8. js 实现页面点击按钮复制内容

    前言: 我们平时在页面中是按照长按来实现复制相关的内容,那么怎么用js实现点击按钮实现复制相关的内容呢?请看如下方法: 实现步骤: 1.引入相关的js(ClipboardJS插件) <scrip ...

  9. MVC框架与增强

    一.什么是MVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示 ...

  10. exit命令

    exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行. 常用参数格式:exit n退出.设置退出码为n.(Cause the shell to exit with a statu ...