zoukankan      html  css  js  c++  java
  • 641.设计循环双端队列

    2020-04-10
    设计循环双端队列

    设计实现双端队列。

    你的实现需要支持以下操作:

    MyCircularDeque(k):构造函数,双端队列的大小为k。

    insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。

    insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。

    deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。

    deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。

    getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。

    getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回 -1。

    isEmpty():检查双端队列是否为空。

    isFull():检查双端队列是否满了。

    题解:
    思路1: 数组
    /**
     * Initialize your data structure here. Set the size of the deque to be k.
     * @param {number} k
     */
    var MyCircularDeque = function (k) {
      this.queue = [];
      this.length = k;
    };
    
    /**
     * Adds an item at the front of Deque. Return true if the operation is successful. 
     * @param {number} value
     * @return {boolean}
     */
    MyCircularDeque.prototype.insertFront = function (value) {
      if (this.queue.length < this.length) {
        this.queue.unshift(value);
        return true;
      }
      return false;
    };
    
    /**
     * Adds an item at the rear of Deque. Return true if the operation is successful. 
     * @param {number} value
     * @return {boolean}
     */
    MyCircularDeque.prototype.insertLast = function (value) {
      if (this.queue.length < this.length) {
        this.queue.push(value);
        return true;
      }
      return false;
    };
    
    /**
     * Deletes an item from the front of Deque. Return true if the operation is successful.
     * @return {boolean}
     */
    MyCircularDeque.prototype.deleteFront = function () {
      if (this.queue.length > 0) {
        this.queue.shift();
        return true;
      }
      return false;
    };
    
    /**
     * Deletes an item from the rear of Deque. Return true if the operation is successful.
     * @return {boolean}
     */
    MyCircularDeque.prototype.deleteLast = function () {
      if (this.queue.length > 0) {
        this.queue.pop();
        return true;
      }
      return false;
    };
    
    /**
     * Get the front item from the deque.
     * @return {number}
     */
    MyCircularDeque.prototype.getFront = function () {
      if (this.queue.length > 0) return this.queue[0];
      return -1;
    };
    
    /**
     * Get the last item from the deque.
     * @return {number}
     */
    MyCircularDeque.prototype.getRear = function () {
      if (this.queue.length > 0) return this.queue[this.queue.length - 1];
      return -1;
    };
    
    /**
     * Checks whether the circular deque is empty or not.
     * @return {boolean}
     */
    MyCircularDeque.prototype.isEmpty = function () {
      return this.queue.length === 0;
    };
    
    /**
     * Checks whether the circular deque is full or not.
     * @return {boolean}
     */
    MyCircularDeque.prototype.isFull = function () {
      return this.queue.length === this.length;
    };
    
    /**
     * Your MyCircularDeque object will be instantiated and called as such:
     * var obj = new MyCircularDeque(k)
     * var param_1 = obj.insertFront(value)
     * var param_2 = obj.insertLast(value)
     * var param_3 = obj.deleteFront()
     * var param_4 = obj.deleteLast()
     * var param_5 = obj.getFront()
     * var param_6 = obj.getRear()
     * var param_7 = obj.isEmpty()
     * var param_8 = obj.isFull()
     */
  • 相关阅读:
    ubuntu安装QQ
    Ubuntu 14.04/14.10下安装VMware Workstation
    在Macbook上安装ubuntu
    MacBook Air密码忘了,苹果电脑密码忘了怎么办
    Win7下U盘安装Ubuntu14.04双系统
    linux紧急救援模式
    Standard Library Modules Using Notes
    【LeetCode】136 & 137 & 260
    Python获取脚本所在目录的正确方法【转】
    Autorun a python script after reboot using rc.local
  • 原文地址:https://www.cnblogs.com/lanpang9661/p/12671208.html
Copyright © 2011-2022 走看看