设计实现双端队列。

你的实现需要支持以下操作:

MyCircularDeque(k):构造函数,双端队列的大小为k。insertFront():将一个元素添加到双端队列头部。如果操作成功返回 true。insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。deleteFront():从双端队列头部删除一个元素。如果操作成功返回 true。deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。getRear():获得双端队列的最后一个元素。如果双端队列为空,返回 -1。isEmpty():检查双端队列是否为空。isFull():检查双端队列是否满了。示例:MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3circularDeque.insertLast(1);              // 返回 truecircularDeque.insertLast(2);              // 返回 truecircularDeque.insertFront(3);              // 返回 truecircularDeque.insertFront(4);              // 已经满了,返回 falsecircularDeque.getRear();          // 返回 2circularDeque.isFull();                // 返回 truecircularDeque.deleteLast();              // 返回 truecircularDeque.insertFront(4);              // 返回 truecircularDeque.getFront();        // 返回 4

提示:

所有值的范围为 [1, 1000]

操作次数的范围为 [1, 1000]

请不要使用内置的双端队列库。

解题思路

1、定义循环变量 front 和 rear 。一直保持这个定义,到底是先赋值还是先移动指针就很容易想清楚了。

front:指向队列头部第 1 个有效数据的位置;

rear:指向队列尾部(即最后 1 个有效数据)的下一个位置,即下一个从队尾入队元素的位置。

(说明:这个定义是依据“动态数组”的定义模仿而来。)

2、为了避免“队列为空”和“队列为满”的判别条件冲突,我们有意浪费了一个位置。

浪费一个位置是指:循环数组中任何时刻一定至少有一个位置不存放有效元素。

判别队列为空的条件是:front == rear;

判别队列为满的条件是:(rear + 1) % capacity == front;。可以这样理解,当 rear 循环到数组的前面,要从后面追上 front,还差一格的时候,判定队列为满。

3、因为有循环的出现,要特别注意处理数组下标可能越界的情况。

(1)指针后移的时候,索引 + 1,要取模;

(2)指针前移的时候,为了循环到数组的末尾,需要先加上数组的长度,然后再对数组长度取模。

4,如果队列为空

插入元素的时候要注意,

(1)头部插入,rear 后移

(2)尾部插入,rear后移

代码实现

type MyCircularDeque struct {    length int    data []int    head int    rear int}/** Initialize your data structure here. Set the size of the deque to be k. */func Constructor(k int) MyCircularDeque {    return MyCircularDeque{        length:k+1,        data:make([]int,k+1), //空一个位置区分满和空        head:0,        rear:0,    }}/** Adds an item at the front of Deque. Return true if the operation is successful. */func (this *MyCircularDeque) InsertFront(value int) bool {    if this.IsFull(){       return false   }   if this.IsEmpty(){       if this.rear==this.length-1{           this.rear=0       }else{           this.rear++       }       this.data[this.head]=value       return true   }      if this.head==0{       this.head=this.length-1   }else{       this.head--   }    this.data[this.head]=value   return true}/** Adds an item at the rear of Deque. Return true if the operation is successful. */func (this *MyCircularDeque) InsertLast(value int) bool {  if this.IsFull(){      return false  }  if this.IsEmpty(){      this.data[this.rear]=value     if this.rear==this.length-1{          this.rear=0     }else{         this.rear++     }     return true  }    this.data[this.rear]=value  if this.rear==this.length-1{      this.rear=0  }else{      this.rear++  }  return true}/** Deletes an item from the front of Deque. Return true if the operation is successful. */func (this *MyCircularDeque) DeleteFront() bool {  if this.IsEmpty(){      return false  }  if this.head==this.length-1{      this.head=0  }else{      this.head++  }  return true}/** Deletes an item from the rear of Deque. Return true if the operation is successful. */func (this *MyCircularDeque) DeleteLast() bool {    if this.IsEmpty(){        return false    }    if this.rear==0{        this.rear=this.length-1    }else{        this.rear--    }    return true}/** Get the front item from the deque. */func (this *MyCircularDeque) GetFront() int {        if this.IsEmpty(){            return -1        }        return this.data[this.head]  }/** Get the last item from the deque. */func (this *MyCircularDeque) GetRear() int {        if this.IsEmpty(){            return -1        }        if this.rear==0{            return this.data[this.length-1]        }      return this.data[this.rear-1]}/** Checks whether the circular deque is empty or not. */func (this *MyCircularDeque) IsEmpty() bool {    return this.head==this.rear}/** Checks whether the circular deque is full or not. */func (this *MyCircularDeque) IsFull() bool {    return (this.rear+1)%this.length==this.head}/** * Your MyCircularDeque object will be instantiated and called as such: * obj := Constructor(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(); */

推荐阅读

  • Go 刷 LeetCode 系列:经典(6) 实现跳表


喜欢本文的朋友,欢迎关注“Go语言中文网”:

Go语言中文网启用微信学习交流群,欢迎加微信:274768166,投稿亦欢迎

sv队列和动态数组的区别_Go 刷 LeetCode 系列:经典(7) 设计双端队列相关推荐

  1. C#LeetCode刷题之#641-设计循环双端队列(Design Circular Deque)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4132 访问. 设计实现双端队列. 你的实现需要支持以下操作: M ...

  2. sv队列和动态数组的区别_systemverilog中几种数组类型的基础知识

    在开始今天的内容之前,先来一个小玩具,一条用来删除文件的bat语句.我发现questasim跟vim同时对文本进行修改的时候,同目录下会产生很多很多很多的中间文件,所以很久不用的bat又要搬出来了.语 ...

  3. sv队列和动态数组的区别_systemverilog学习(4)动态数组

    本节主要内容:动态数组,队列,联合数组,数组基本操作,结构体类型,枚举类型 一:动态数组 1:基础 在run-time才知道元素个数,在compile-time不知道 可以在仿真的时候再确定元素个数 ...

  4. 二叉搜索树的中序遍历为 递增序列_Go 刷 Leetcode 系列:恢复二叉搜索树

    二叉搜索树中的两个节点被错误地交换. 请在不改变其结构的情况下,恢复这棵树. 示例 1: 输入: [1,3,null,null,2] 1 / 3 \ 2输出: [3,1,null,null,2] 3 ...

  5. 二叉树和等于某值路径_Go刷LeetCode系列:二叉树(3)二叉树路径和

    给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例:  给定如下二叉树,以及目标和 sum = 2 ...

  6. 《恋上数据结构第1季》队列、双端队列、循环队列、循环双端队列

    队列(Queue) 队列 Queue 队列的接口设计 队列源码 双端队列 Deque 双端队列接口设计 双端队列源码 循环队列 Circle Queue 循环队列实现 索引映射封装 循环队列 – %运 ...

  7. ArrayDeque(双端队列的线性实现)详解

    ArrayDeque 是 java 中对双端队列的线性实现 一. 特性 无容量大小限制,容量按需增长: 非线程安全队列,无同步策略,不支持多线程安全访问: 当用作栈时,性能优于Stack,当用于队列时 ...

  8. Boring data structure problem 模拟-双端队列

    题意 : 维护一个"双端队列",1e7次操作,支持左插入,右插入,按值删除,查找中点(靠右)值ceil[(m+1)/2]ceil[(m + 1) / 2]ceil[(m+1)/2] ...

  9. Java 集合深入理解(10):Deque 双端队列

    点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 什么是 Deque Deque 是 Double ended queue (双端队列) 的缩写,读音和 deck 一样,蛋 ...

最新文章

  1. 最古老的100个.com域名
  2. 让你的man手册显示与众不同
  3. todo文件说明已停止工作_番茄ToDo,一款颜值功能兼备的番茄钟。
  4. sublime text 3在windows中配置ctags插件
  5. 大端模式小端模式 主机序网络序
  6. ASP.NET Core 2.1 : 图解路由(2.1 or earler)
  7. linux nginx编译详解,Linux下nginx编译安装教程和编译参数详解
  8. 一个可变参数类型检查的示例
  9. 物质之学 —— 等离子(物质的第四态)
  10. [转载] python sorted 使用cmp函数时候注意cmp需要传入两个参数,传入两个参数机制的分析
  11. elasticsearch的性能表现
  12. 前期易语言编程作品收录|赤壁盗号|
  13. React的调和过程
  14. c语言74hc595程序,单片机驱动74HC595的c51程序 - 51单片机控制74HC595驱动的编程要点_单片机驱动74HC595的c51程序...
  15. VulnHub-noob打靶记录
  16. ChineseLunisolarCalendar 农历日期
  17. vue+springboot通过post请求实现文件下载
  18. [MCSM] Slice Sampler
  19. 软文营销登顶销售奇迹的4U定律你知道吗?
  20. legacy引导gpt分区_安装win10用uefi还是legacy引导模式?(最全分析)

热门文章

  1. .NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  2. 一文带你了解华为云DevCloud为何能全面领跑中国DevOps云服务市场
  3. 程序员后期,架构师发展路线!
  4. [译]RabbitMQ教程C#版 - 远程过程调用(RPC)
  5. ASP.NET 开发者 开始学习ASP.NET Core 2吧
  6. Asp.Net Core 发布和部署(Linux + Jexus )
  7. .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门
  8. Mysql数据库安全性问题【防注入】
  9. 【ArcGIS风暴】在ArcGIS中实现将一个圆16等分
  10. 地理知识归纳:影响降水的九大因素