2-4 LC641 设计循环双端队列 字节校园每日一题 Java力扣刷题笔记

LC641 设计循环双端队列

我的刷题笔记
设计循环双端队列

1.读题

设计实现双端队列

本题我们要实现支持如下操作的方法

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

在这里插入图片描述

2.解题思路

本题跟LC622 设计循环队列思想是一样der~
点链接康康我的题解吧~

因为是双向循环队列 所以两头都可以出队/入队
我们称双向队列为deque~ 即为 double-ended-queue

要注意一个主要的不同 就是双端队列时两边都可以入队的!(所以有insertfrontinsertlast!!)
而我们的头指针其实是指向一个实际值的 所以在插入队头的时候不可以直接插 需要先将指针前移 再去插

最开始很疑惑这里为啥要先减再插
在这里插入图片描述
感谢这位大佬的讲解~
在这里插入图片描述

3.代码逻辑

给出力扣大佬的一个图解 看完这个整个代码逻辑估计就很清晰了!

4.Java代码

class MyCircularDeque {
    int arr [];
    int count, head, tail;
    int size;//需要定义队列的长度
    /** Initialize your data structure here. Set the size of the deque to be k. */
    public MyCircularDeque(int k) {
        this.arr = new int[k];
        this.head = 0;
        this.tail = 0;
        this.count = 0;//队列当前元素数量
        this.size = k;
    }
    
    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    // 将元素添加到头部
    public boolean insertFront(int value) {
        if (isFull()) return false;
        //因为头指针指向的位置有元素 所以需要先将头指针前移
        // head = (head - 1 + size) % size;下面两行和这行作用相同 防止head指针指向-1 导致不合法
        head = head - 1;//head指针向前走一步
        if (head == -1) head = size - 1;//如果前面没位置了 那就指向尾部
        arr[head] = value;
        count += 1;//插入之后 队列中元素数量+1
        return true;
    }
    
    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    public boolean insertLast(int value) {
        if (isFull()) return false;//队列满了就退出~
        arr[tail] = value;//进行入队
        // tail = (tail + 1) % size;这行和下面两行作用是一样的! 都是让尾指针循环起来
        tail += 1;
        if (tail == size) tail = 0;//tail指针指向最后一位 则从头开始
        count += 1;
        return true;
    }
    
    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    public boolean deleteFront() {
        if(isEmpty()) return false;
        head = (head +1) % size;//这里可以看到 用到了上面说的循环指针的方法~
        count -= 1;
        return true;
    }
    
    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    public boolean deleteLast() {
        if(isEmpty()) return false;
        tail = (tail - 1 + size) % size;//用到了上面说的循环指针的方法~
        //如果是前移指针 一定要这么操作!防止指针指向-1 出现不合法情况
        count -= 1;
        return true;
    }
    
    /** Get the front item from the deque. */
    public int getFront() {
        if (isEmpty()) return -1;
        return arr[head];
    }
    
    /** Get the last item from the deque. */
    public int getRear() {
        if (isEmpty()) return -1;
        return arr[(tail - 1 + size) % size] ;
    }
    
    /** Checks whether the circular deque is empty or not. */
    public boolean isEmpty() {
        return count == 0;//判断当前元素总量是否为0
    }
    
    /** Checks whether the circular deque is full or not. */
    public boolean isFull() {
        return size == count;//有了计数器 判满如此简单~~~
    }
}

【字节校园每日一题】LC143 重排链表

我的刷题笔记
重排链表
今天群里小伙伴表示美团面试考到了原题hhh
在这里插入图片描述
艾玛看来要好好写每日一题了鸭

1.读题

在这里插入图片描述
待解决!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值