Leetcode 641.设计循环双端队列

题目详情

Leetcode 641.设计循环双端队列_第1张图片
Leetcode 641.设计循环双端队列_第2张图片
Leetcode 641.设计循环双端队列_第3张图片

解题思路

因为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.设计循环双端队列_第4张图片

你可能感兴趣的:(力扣题目,数据结构与算法)