资讯专栏INFORMATION COLUMN

LeetCode 之 JavaScript 解答第641题 —— 设计双端队列(Design Cir

Freeman / 1859人阅读

摘要:小鹿题目设计实现双端队列。你的实现需要支持以下操作构造函数双端队列的大小为。获得双端队列的最后一个元素。检查双端队列是否为空。数组头部删除第一个数据。以上数组提供的使得更方便的对数组进行操作和模拟其他数据结构的操作,栈队列等。

Time:2019/4/15
Title: Design Circular Deque
Difficulty: Medium
Author: 小鹿

题目:Design Circular Deque

Design your implementation of the circular double-ended queue (deque).

Your implementation should support following operations:

MyCircularDeque(k): Constructor, set the size of the deque to be k.

insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.

insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.

deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.

deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.

getFront(): Gets the front item from the Deque. If the deque is empty, return -1.

getRear(): Gets the last item from Deque. If the deque is empty, return -1.

isEmpty(): Checks whether Deque is empty or not.

isFull(): Checks whether Deque is full or not.

设计实现双端队列。
你的实现需要支持以下操作:

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

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

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

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

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

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

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

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

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

Example:

MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3
circularDeque.insertLast(1);            // return true
circularDeque.insertLast(2);            // return true
circularDeque.insertFront(3);            // return true
circularDeque.insertFront(4);            // return false, the queue is full
circularDeque.getRear();              // return 2
circularDeque.isFull();                // return true
circularDeque.deleteLast();            // return true
circularDeque.insertFront(4);            // return true
circularDeque.getFront();            // return 4

Note:

All values will be in the range of [0, 1000].

The number of operations will be in the range of [1, 1000].

Please do not use the built-in Deque library.

Solve:
▉ 算法思路

借助 Javascript 中数组中的 API 可快速实现一个双向队列。如:

arr.pop() : 删除数组尾部最后一个数据。

arr.push() :在数组尾部插入一个数据。

arr.shift():数组头部删除第一个数据。

arr.unshift():数组头部插入一个数据。

以上数组提供的 API 使得更方便的对数组进行操作和模拟其他数据结构的操作,栈、队列等。

▉ 代码实现
 //双端列表构造
        var MyCircularDeque = function(k) {
            this.deque = [];
            this.size = 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.deque.length === this.size){
                return false;
            }else{
                this.deque.unshift(value);
                return true;
            }
        };

        /**
        * 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.deque.length === this.size){
                return false;
            }else{
                this.deque.push(value);
                return true;
            }
        };

        /**
        * Deletes an item from the front of Deque. Return true if the operation is successful.
        * @return {boolean}
        * 功能:队列头部出队
        */
        MyCircularDeque.prototype.deleteFront = function() {
            if(this.deque.length === 0){
                return false;
            }else{
                this.deque.shift();
                return true;
            }
        };

        /**
        * Deletes an item from the rear of Deque. Return true if the operation is successful.
        * @return {boolean}
        * 功能:队列尾部出队
        */
        MyCircularDeque.prototype.deleteLast = function() {
            if(this.deque.length === 0){
                return false;
            }else{
                this.deque.pop();
                return true;
            }
        };

        /**
        * Get the front item from the deque.
        * @return {number}
        * 功能:获取队列头部第一个数据
        */
        MyCircularDeque.prototype.getFront = function() {
            if(this.deque.length === 0){
                return -1;
            }else{
                return this.deque[0];
            }
        };

        /**
        * Get the last item from the deque.
        * @return {number}
        * 功能:获取队列尾部第一个数据
        */
        MyCircularDeque.prototype.getRear = function() {
            if(this.deque.length === 0){
                return -1;
            }else{
                return this.deque[this.deque.length - 1];
            }
        };

        /**
        * Checks whether the circular deque is empty or not.
        * @return {boolean}
        * 功能:判断双端队列是否为空
        */
        MyCircularDeque.prototype.isEmpty = function() {
            if(this.deque.length === 0){
                return true;
            }else{
                return false;
            }
        };

        /**
        * Checks whether the circular deque is full or not.
        * @return {boolean}
        * 功能:判断双端队列是否为满
        */
        MyCircularDeque.prototype.isFull = function() {
            if(this.deque.length === this.size){
                return true;
            }else{
                return false;
            }
        };

        //测试
        var obj = new MyCircularDeque(3)
        var param_1 = obj.insertFront(1)
        var param_2 = obj.insertLast(2)
        console.log("-----------------------------插入数据------------------------")
        console.log(`${param_1}${param_2}`)
        var param_3 = obj.deleteFront()
        var param_4 = obj.deleteLast()
        console.log("-----------------------------删除数据------------------------")
        console.log(`${param_3}${param_4}`)
        var param_5 = obj.getFront()
        var param_6 = obj.getRear()
        console.log("-----------------------------获取数据------------------------")
        console.log(`${param_5}${param_6}`)
        var param_7 = obj.isEmpty()
        var param_8 = obj.isFull()
        console.log("-----------------------------判断空/满------------------------")
        console.log(`${param_7}${param_8}`)


欢迎一起加入到 LeetCode 开源 Github 仓库,可以向 me 提交您其他语言的代码。在仓库上坚持和小伙伴们一起打卡,共同完善我们的开源小仓库!
Github:https://github.com/luxiangqia...
欢迎关注我个人公众号:「一个不甘平凡的码农」,记录了自己一路自学编程的故事。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/103589.html

相关文章

  • LeetCode JavaScript 解答239 —— 滑动窗口最大值(Sliding W

    摘要:你只可以看到在滑动窗口内的数字。滑动窗口每次只向右移动一位。返回滑动窗口最大值。算法思路暴力破解法用两个指针,分别指向窗口的起始位置和终止位置,然后遍历窗口中的数据,求出最大值向前移动两个指针,然后操作,直到遍历数据完成位置。 Time:2019/4/16Title: Sliding Window MaximumDifficulty: DifficultyAuthor: 小鹿 题目...

    spacewander 评论0 收藏0
  • LeetCode JavaScript 解答104 —— 二叉树的最大深度

    摘要:小鹿题目二叉树的最大深度给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。求二叉树的深度,必然要用到递归来解决。分别递归左右子树。 Time:2019/4/22Title: Maximum Depth of Binary TreeDifficulty: MediumAuthor:小鹿 题目:Maximum Depth of Binary Tre...

    boredream 评论0 收藏0
  • LeetCode JavaScript 解答226 —— 翻转二叉树(Invert Bina

    摘要:算法思路判断树是否为空同时也是终止条件。分别对左右子树进行递归。代码实现判断当前树是否为左右子树结点交换分别对左右子树进行递归返回树的根节点欢迎一起加入到开源仓库,可以向提交您其他语言的代码。 Time:2019/4/21Title: Invert Binary TreeDifficulty: EasyAuthor: 小鹿 题目:Invert Binary Tree(反转二叉树) ...

    MingjunYang 评论0 收藏0

发表评论

0条评论

Freeman

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<