LeetCode.M641.设计循环双端队列

本文讲解了如何使用O(1)时间复杂度和O(n)空间复杂度设计循环双端队列,介绍了两种解法并对比其优劣,适合LeetCode 641题目的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode.M641

题目:

在这里插入图片描述

题目大意:

模拟双端队列。空间上是循环的。

数据范围:

1 <= k <= 1000
0 <= value <= 1000
insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull  调用次数不大于 2000

一 、不推荐解法 :

思路:

这是空间上不循环的。

代码:

class MyCircularDeque {

    int[] deque;
    int maxlen, l, r;

    public MyCircularDeque(int k) {
        deque = new int[2020];
        maxlen = k;
        l = 1000;
        r = 1000;
    }

    public boolean insertFront(int value) {
        if (!isFull()){
            deque[-- l] = value;
            return true;
        }
        return false;
    }

    public boolean insertLast(int value) {
        if (!isFull()){
            deque[r ++ ] = value;
            return true;
        }
        return false;
    }

    public boolean deleteFront() {
        if (!isEmpty()){
            l ++ ;
            return true;
        }
        return false;
    }

    public boolean deleteLast() {
        if (!isEmpty()){
            r -- ;
            return true;
        }
        return false;
    }

    public int getFront() {
        if (!isEmpty()){
            return deque[l];
        }
        return -1;
    }

    public int getRear() {
        if (!isEmpty()){
            return deque[r - 1];
        }
        return -1;
    }

    public boolean isEmpty() {
        return l == r ? true : false;
    }

    public boolean isFull() {
        return r - l  == maxlen ? true : false;
    }
}

时空复杂度分析等:

  • 时间复杂度 : 每个操作都是O(1)
  • 空间复杂度 : O(n)

二 、推荐解法 :

思路:

队头为l,队尾为r,l代表了队头元素所处的位置,r代表了队尾元素的下一个位置。为了将队空和队满判断条件分开,需空闲一个位置,所以初始化大小为capacity + 1大小的数组。所以操作为:

  • 初始化:

    public MyCircularDeque(int k) {
    	l = 0;
    	r = 0;
    	capacity = k + 1; //容量加1,区分empty和full的判断条件
    	deque = new int[capacity];
    }
    
  • 判空:

    public boolean isEmpty() {
        return l == r ? true : false;
    }
    
  • 判满:

    public boolean isFull() {
        return (r + 1) % capacity  == l ? true : false;
    }
    
  • 插入队头 :

    public boolean insertFront(int value) {
    	if (!isFull()){
    		l = (l - 1 + capacity) % capacity;
    		deque[l] = value;
    		return true;
    	}
    	return false;
    }
    
  • 删除队头:

    public boolean deleteFront() {
        if (!isEmpty()){
            l = (l + 1) % capacity;
            return true;
        }
        return false;
    }
    
  • 获得队头:

    public int getFront() {
        if (!isEmpty()){
            return deque[l];
        }
        return -1;
    }
    
  • 插入队尾 :

    public boolean insertLast(int value) {
    	if (!isFull()){
    		deque[r] = value;
    		r = (r + 1) % capacity;
    		return true;
    	}
    	return false;
    }
    
  • 删除队尾:

    public boolean deleteLast() {
    	if (!isEmpty()){
    		r = (r - 1 + capacity) % capacity;
    		return true;
    	}
       return false;
    }
    
  • 获得队尾:

    public int getRear() {
    	if (!isEmpty()){
    		return deque[(r - 1 + capacity) % capacity];
    	}
    	return -1;
    }
    

代码:

class MyCircularDeque {

    int[] deque;
    int capacity, l, r;

    public MyCircularDeque(int k) {
        l = 0;
        r = 0;
        capacity = k + 1; //容量加1,区分empty和full的判断条件
        deque = new int[capacity];
    }

    public boolean insertFront(int value) {
        if (!isFull()){
            l = (l - 1 + capacity) % capacity;
            deque[l] = value;
            return true;
        }
        return false;
    }

    public boolean insertLast(int value) {
        if (!isFull()){
            deque[r] = value;
            r = (r + 1) % capacity;
            return true;
        }
        return false;
    }

    public boolean deleteFront() {
        if (!isEmpty()){
            l = (l + 1) % capacity;
            return true;
        }
        return false;
    }

    public boolean deleteLast() {
        if (!isEmpty()){
            r = (r - 1 + capacity) % capacity;
            return true;
        }
        return false;
    }

    public int getFront() {
        if (!isEmpty()){
            return deque[l];
        }
        return -1;
    }

    public int getRear() {
        if (!isEmpty()){
            return deque[(r - 1 + capacity) % capacity];
        }
        return -1;
    }

    public boolean isEmpty() {
        return l == r ? true : false;
    }

    public boolean isFull() {
        return (r + 1) % capacity  == l ? true : false;
    }
}

时空复杂度分析等:

  • 时间复杂度 : 每个操作都为O(1)

  • 空间复杂度 : O(n)

题目链接:

641. 设计循环双端队列 - 力扣(LeetCode)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值