前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >golang刷leetcode 经典(7) 设计双端队列

golang刷leetcode 经典(7) 设计双端队列

作者头像
golangLeetcode
发布2022-08-02 17:33:18
3210
发布2022-08-02 17:33:18
举报
文章被收录于专栏:golang算法架构leetcode技术php

设计实现双端队列。

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

代码语言:javascript
复制
MyCircularDeque(k):构造函数,双端队列的大小为k。
insertFront():将一个元素添加到双端队列头部。如果操作成功返回 true。
insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。
deleteFront():从双端队列头部删除一个元素。如果操作成功返回 true。
deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。
getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。
getRear():获得双端队列的最后一个元素。如果双端队列为空,返回 -1。
isEmpty():检查双端队列是否为空。
isFull():检查双端队列是否满了。
示例:


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, 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后移

代码实现

代码语言:javascript
复制
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();
 */
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-12-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 golang算法架构leetcode技术php 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档