欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Leetcode 641.设计循环双端队列

程序员文章站 2022-04-04 08:29:50
...

题目详情

Leetcode 641.设计循环双端队列
Leetcode 641.设计循环双端队列
Leetcode 641.设计循环双端队列

解题思路

因为queue不能头插,而list插入方式*,所以可用list实现

代码实现

(C#)

(List 实现)

public class MyCircularDeque {
    List<int> lst ;
    int len;
    /** Initialize your data structure here. Set the size of the deque to be k. */
    public MyCircularDeque(int k) {
        lst = new List<int>(k);
        len = k;
    }
    
    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    public bool InsertFront(int value) {
         if(lst.Count==len) return false;
        else
        {
            lst.Insert(0,value);
            return true;
        }
    }
    
    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    public bool InsertLast(int value) {
        if(lst.Count==len) return false;
        else
        {
            lst.Add(value);
            return true;
        }
    }
    
    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    public bool DeleteFront() {
        if(lst.Count == 0) return false;
        else
        {
            lst.RemoveAt(0);
            return true;
        }
    }
    
    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    public bool DeleteLast() {
        if(lst.Count == 0) return false;
        else
        {
            lst.RemoveAt(lst.Count-1);
            return true;
        }
    }
    
    /** Get the front item from the deque. */
    public int GetFront() {
        if(lst.Count == 0) return -1;
        else  return lst[0];
        
    }
    
    /** Get the last item from the deque. */
    public int GetRear() {
        if(lst.Count == 0) return -1;
        else return lst[lst.Count-1];
    }
    
    /** Checks whether the circular deque is empty or not. */
    public bool IsEmpty() {
        if(lst.Count == 0) return true;
        else return false;
    }
    
    /** Checks whether the circular deque is full or not. */
    public bool IsFull() {
        if(lst.Count == len) return true;
        else return false;
    }
}

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * bool param_1 = obj.InsertFront(value);
 * bool param_2 = obj.InsertLast(value);
 * bool param_3 = obj.DeleteFront();
 * bool param_4 = obj.DeleteLast();
 * int param_5 = obj.GetFront();
 * int param_6 = obj.GetRear();
 * bool param_7 = obj.IsEmpty();
 * bool param_8 = obj.IsFull();
 */

Leetcode 641.设计循环双端队列