美文网首页硬核空间技术博客
leetcode641.设计循环双端队列

leetcode641.设计循环双端队列

作者: 憨憨二师兄 | 来源:发表于2020-04-26 14:32 被阅读0次

    题目链接

    题解:

    类似的题目为:leetcode622题设计循环队列,622题的 题解 先附上。
    本题和622题是一样的,注意的点也在我的题解上有详细的说明,在这里就不重复了。底层依旧是使用数组,以及两个指针front,tail分别标记队首元素和下一个addLast的添加位置。代码如下:

    class MyCircularDeque {
    
        private int[] data;
        private int front;
        private int tail;
    
        /** Initialize your data structure here. Set the size of the deque to be k. */
        public MyCircularDeque(int k) {
            data = new int[k + 1];
            front = 0;
            tail = 0;
        }
        
        private int getSize(){
            if(tail < front){
                return data.length - front + tail;
            }else{
                return tail - front;
            }
        }
    
        /** Adds an item at the front of Deque. Return true if the operation is successful. */
        public boolean insertFront(int value) {
            if(isFull()){
                return false;
            }else{
                front = (front + data.length - 1) % data.length;
                data[front] = value;
                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;
            }else{
                data[tail] = value;
                tail = (tail + 1) % data.length;
                return true;
            }
        }
        
        /** Deletes an item from the front of Deque. Return true if the operation is successful. */
        public boolean deleteFront() {
            if(getSize() == 0){
                return false;
            }else{
                front = (front + 1) % data.length;
                return true;
            }
        }
        
        /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
        public boolean deleteLast() {
            if(getSize() == 0){
                return false;
            }else{
                tail = (tail + data.length - 1) % data.length;
                return true;
            }
        }
        
        /** Get the front item from the deque. */
        public int getFront() {
            if(front == tail){
                return -1;
            }else{
                return data[front];
            }
        }
        
        /** Get the last item from the deque. */
        public int getRear() {
            if(front == tail){
                return -1;
            }else{
                return data[(front + getSize() - 1) % data.length];
            }
        }
        
        /** Checks whether the circular deque is empty or not. */
        public boolean isEmpty() {
            return front == tail;
        }
        
        /** Checks whether the circular deque is full or not. */
        public boolean isFull() {
            return getSize() == data.length - 1;
        }
    }
    
    /**
     * 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();
     */
    

    执行结果如下:


    相关文章

      网友评论

        本文标题:leetcode641.设计循环双端队列

        本文链接:https://www.haomeiwen.com/subject/ceydwhtx.html