• 641.设计双向循环队列


    题目

    设计实现双端队列

    实现 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
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    提示:

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

    Related Topics

    • 设计
    • 队列
    • 数组
    • 链表

    来源:力扣(LeetCode
    链接:https://leetcode.cn/problems/design-circular-deque
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    思路

    数组 or 链表

    链表要比数组简单一点

    题解

    数组:

    class MyCircularDeque {
        private int[] arr;
        private int rear,front;
        private int capacity;
        
        public MyCircularDeque(int k) {
            capacity = k + 1;
            rear = 0;
            front = 0;
            arr = new int[k + 1];
        }
        
        public boolean insertFront(int value) {
            if (isFull()){
                return false;
            }
            front = (front - 1 + capacity) % capacity;
            arr[front] = value;
            return true;
        }
        
        public boolean insertLast(int value) {
            if (isFull()){
                return false;
            }
            arr[rear] = value;
            rear = (rear + 1) % capacity;
            return true;
        }
        
        public boolean deleteFront() {
            if (isEmpty()){
                return false;
            }
            front = (front + 1) % capacity;
            return true;
        }
        
        public boolean deleteLast() {
            if (isEmpty()){
                return false;
            }
            rear = (rear - 1 + capacity) % capacity;
            return true;
        }
        
        public int getFront() {
            if (isEmpty()){
                return -1;
            }else {
                return arr[front];
            }
        }
        
        public int getRear() {
            if (isEmpty()){
                return -1;
            }else {
                return arr[(rear - 1 + capacity) % capacity];
            }
        }
        
        public boolean isEmpty() {
            return rear == front;
        }
        
        public boolean isFull() {
            return (rear + 1) % capacity == front;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    链表:

    class MyCircularDeque {
    
        private class LinkedListNode{
            int val;
            LinkedListNode prev,next;
            LinkedListNode(int val){
                this.val = val;
            }
        }
    
        private LinkedListNode head,tail;//头、尾
        private int capacity;//总容量
        private int size;//当前长度
    
        public MyCircularDeque(int k) {
            capacity = k;
            size = 0;
        }
    
        public boolean insertFront(int value) {
            if (isFull()){
                return false;
            }
            LinkedListNode node = new LinkedListNode(value);
            if (size == 0){
                head = tail = node;
            }else {
                node.next = head;
                head.prev = node;
                head = node;
            }
            size++;
            return true;
        }
    
        public boolean insertLast(int value) {
            if (isFull()){
                return false;
            }
            LinkedListNode node = new LinkedListNode(value);
            if (size == 0){
                head = tail = node;
            }else {
                node.prev = tail;
                tail.next = node;
                tail = node;
            }
            size++;
            return true;
        }
    
        public boolean deleteFront() {
            if (isEmpty()){
                return false;
            }
            head = head.next;
            size--;
            return true;
        }
    
        public boolean deleteLast() {
            if (isEmpty()){
                return false;
            }
            tail = tail.prev;
            size--;
            return true;
        }
    
        public int getFront() {
            if (isEmpty()) return -1;
            return head.val;
        }
    
        public int getRear() {
            if (isEmpty()) return -1;
            return tail.val;
        }
    
        public boolean isEmpty() {
    
            return size == 0;
        }
    
        public boolean isFull() {
            return size == capacity;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
  • 相关阅读:
    PCL点云处理之点云转为Mesh模型并保存---方法一 (二百一十)
    ElasticSearch 入门
    Oracle/PLSQL: LNNVL Function
    Zynq UltraScale+ XCZU3EG 纯VHDL解码 IMX214 MIPI 视频,2路视频拼接输出,提供vivado工程源码和技术支持
    JavaScript保姆级教程 ——— 重难点详细解析(万字长文,建议收藏)
    恭喜磊哥喜提n+1
    ubuntu18.04服务器双网口配置上外网
    PHP 获取类对象的信息
    深度学习:如何静悄悄地改变我们的日常生活
    高精度气象模拟软件WRF(Weather Research Forecasting)
  • 原文地址:https://blog.csdn.net/qq_52476654/article/details/126340123