LeetCode刷题笔记:641.设计循环双端队列

1. 问题描述

设计实现双端队列。

实现 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 。

2. 解题思路

移动头指针、尾指针

3. 代码实现

class MyCircularDeque {
    private int[] nums;
    private int capacity = 0;
    private int head = 0;
    private int tail;
    public MyCircularDeque(int k) {
        nums = new int[k];
        tail = k - 1;
    }
    
    public boolean insertFront(int value) {
        if (isFull()) {
            return false;
        }
        if (head == 0) {
            head = nums.length - 1;
            nums[head] = value;
        } else {
            head = head - 1;
            nums[head] = value;
        }
        capacity++;
        return true;
    }
    
    public boolean insertLast(int value) {
        if (isFull()) {
            return false;
        }
        if (tail == nums.length - 1) {
            tail = 0;
            nums[tail] = value;
        } else {
            tail = tail + 1;
            nums[tail] = value;
        }
        capacity++;
        return true;
    }
    
    public boolean deleteFront() {
        if (isEmpty()) {
            return false;
        }
        if (head == nums.length - 1) {
            head = 0;
        } else {
            head = head + 1;
        }
        capacity--;
        return true;
    }
    
    public boolean deleteLast() {
        if (isEmpty()) {
            return false;
        }
        if (tail == 0) {
            tail = nums.length - 1;
        } else {
            tail = tail - 1;
        }
        capacity--;
        return true;
    }
    
    public int getFront() {
        if (isEmpty()) {
            return -1;
        }
        return nums[head];
    }
    
    public int getRear() {
        if (isEmpty()) {
            return -1;
        }
        return nums[tail];
    }
    
    public boolean isEmpty() {
        if (capacity == 0) {
            return true;
        }
        return false;
    }
    
    public boolean isFull() {
        if (capacity == nums.length) {
            return true;
        }
        return false;
    }
}

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值