leetcode雕虫小技medium 641. Design Circular Deque

题干: https://leetcode.com/problems/design-circular-deque/

这题跟622类似,算是它的进阶版,要支持双向的queue,大部分代码我都复用622的方案,

唯独要注意的一点是deleteLast和deleteFront要注意边界条件,就是当删完之后队列为空时,不要移动currentHead或者currentTail指针位置。

代码如下:

package com.example.demo.leetcode;

import java.util.Arrays;

class MyCircularDeque {

    int[] content;

    int currentTail = 0;
    int currentHead = 0;

    /** Initialize your data structure here. Set the size of the deque to be k. */
    public MyCircularDeque(int k) {
        content = new int[k];
        Arrays.fill(content, -1);
    }

    private int previousPointer(int index){
        int p = index-1;
        if(p<0){
            p = p+content.length;
        }
        return p;
    }

    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    public boolean insertFront(int value) {
        if(isFull()){
            return false;
        }
        if(content[currentHead]==-1){
            content[currentHead] = value;
            return true;
        }else{
            currentHead=previousPointer(currentHead);
            content[currentHead] = value;
            return true;
        }
    }

    private int nextPointer(int currentPointer){
        int p = currentPointer+1;
        if(p>content.length-1){
            p=p%content.length;
        }
        return p;
    }

    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    public boolean insertLast(int value) {
        if(isFull()){
            return false;
        }
        if(content[currentTail]==-1){
            content[currentTail] = value;
            return true;
        }else{
            currentTail = nextPointer(currentTail);
            content[currentTail]= value;
            return true;
        }
    }

    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    public boolean deleteFront() {
        if(isEmpty()){
            return false;
        }
        content[currentHead]=-1;
        if(content[nextPointer(currentHead)]!=-1){
            currentHead=nextPointer(currentHead);
        }
        return true;
    }

    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    public boolean deleteLast() {
        if(isEmpty()){
            return false;
        }
        content[currentTail] = -1;
        if(content[previousPointer(currentTail)]!=-1){
            currentTail = previousPointer(currentTail);
        }
        return true;
    }

    /** Get the front item from the deque. */
    public int getFront() {
        return content[currentHead];
    }

    /** Get the last item from the deque. */
    public int getRear() {
        return content[currentTail];
    }

    /** Checks whether the circular deque is empty or not. */
    public boolean isEmpty() {
        for(int i=0;i<content.length;i++){
            if(content[i]!=-1){
                return false;
            }
        }
        return true;
    }

    /** Checks whether the circular deque is full or not. */
    public boolean isFull() {
        for(int i=0;i<content.length;i++){
            if(content[i]==-1){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        MyCircularDeque obj = new MyCircularDeque(77);
        boolean param_1 = obj.insertFront(89);
        int param_2 = obj.getRear();
        boolean param_3 = obj.deleteLast();
        int param_4 = obj.getRear();
        boolean param_5 = obj.insertFront(19);
        boolean param_6 = obj.insertFront(23);
        boolean param_7 = obj.insertFront(23);
        boolean param_8 = obj.insertFront(82);
        boolean param_9 = obj.isFull();
        boolean param_10 = obj.insertFront(45);
        boolean param_11 = obj.isFull();
        int param_12 = obj.getRear();

        // expect 19
        System.out.println(param_12);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值