【08.09 每日一题】641. 设计循环双端队列

93 阅读2分钟

1413. 逐步求和得到正数的最小值

设计实现双端队列。

实现 MyCircularDeque 类:

  • MyCircularDeque(int k) :构造函数,双端队列最大为 k 。
  • boolean insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true ,否则返回 false 。
  • boolean insertLast() :将一个元素添加到双端队列尾部。如果操作成功返回 true ,否则返回 false 。
  • boolean deleteFront() :从双端队列头部删除一个元素。 如果操作成功返回 true ,否则返回 false 。
  • boolean deleteLast() :从双端队列尾部删除一个元素。如果操作成功返回 true ,否则返回 false 。
  • int getFront() ):从双端队列头部获得一个元素。如果双端队列为空,返回 -1 。
  • int getRear() :获得双端队列的最后一个元素。 如果双端队列为空,返回 -1 。
  • boolean isEmpty() :若双端队列为空,则返回 true ,否则返回 false  。
  • boolean isFull() :若双端队列满了,则返回 true ,否则返回 false 。

示例1

输入
["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

思路

解法

type MyCircularDeque struct {
    rear int
    front int
    array []int
}


func Constructor(k int) MyCircularDeque {
    m:=MyCircularDeque{}
    m.array=make([]int,k+1)
    return m
}


func (this *MyCircularDeque) InsertFront(value int) bool {
    if this.IsFull(){
        return false
    }
    this.front=(this.front-1+len(this.array))%len(this.array)
    fmt.Println(this.front)
    this.array[this.front]=value
    return true
}


func (this *MyCircularDeque) InsertLast(value int) bool {
    if this.IsFull(){
        return false
    }
    this.array[this.rear]=value
    this.rear=(this.rear+1)%len(this.array)
    return true
}


func (this *MyCircularDeque) DeleteFront() bool {
    if this.IsEmpty(){
        return false
    }
    this.front=(this.front+1)%len(this.array)
    return true
}


func (this *MyCircularDeque) DeleteLast() bool {
    if this.IsEmpty(){
        return false
    }
    this.rear=(this.rear-1+len(this.array))%len(this.array)
    return true
}


func (this *MyCircularDeque) GetFront() int {
    if this.IsEmpty(){
        return -1
    }
    return this.array[this.front]
}


func (this *MyCircularDeque) GetRear() int {
    if this.IsEmpty(){
        return -1
    }
    return this.array[(this.rear-1+len(this.array))%len(this.array)]
}


func (this *MyCircularDeque) IsEmpty() bool {
    if this.rear==this.front{
        return true
    }
    return false
}


func (this *MyCircularDeque) IsFull() bool {
    if (this.rear+1)%len(this.array)==this.front{
        return true
    }
    return false
}


/**
 * 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();
 */

结果

image.png