Java教程

[简单] 622. 设计循环队列

本文主要是介绍[简单] 622. 设计循环队列,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

https://leetcode-cn.com/problems/design-circular-queue/

 

请原谅我直接,这一题。。。我这么傻心里都嘀咕,怎么出得那么傻~没有灵魂得题目。

 

class MyCircularQueue {


    List<Integer> queue = new ArrayList<>();
    Integer capacity = 100000;

    public MyCircularQueue(int k) {
        capacity = k;
    }

    public boolean enQueue(int value) {
        if (queue.size() < capacity) {
            queue.add(value);
            return true;
        }
        return false;
    }

    public boolean deQueue() {
        if(queue.size() == 0) {
            return false;
        }
        queue.remove(0);
        return true;
    }

    public int Front() {
        if(queue.size() == 0) {
            return -1;
        }
        return queue.get(0);
    }

    public int Rear() {
        if(queue.size() == 0) {
            return -1;
        }
        return queue.get(queue.size()  -1);
    }

    public boolean isEmpty() {
        return (queue.size() == 0);
    }

    public boolean isFull() {
        return (queue.size() == capacity);
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */
View Code

 

这篇关于[简单] 622. 设计循环队列的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!