成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

LeetCode 之 JavaScript 解答第641題 —— 設(shè)計雙端隊列(Design Cir

Freeman / 2046人閱讀

摘要:小鹿題目設(shè)計實現(xiàn)雙端隊列。你的實現(xiàn)需要支持以下操作構(gòu)造函數(shù)雙端隊列的大小為。獲得雙端隊列的最后一個元素。檢查雙端隊列是否為空。數(shù)組頭部刪除第一個數(shù)據(jù)。以上數(shù)組提供的使得更方便的對數(shù)組進(jìn)行操作和模擬其他數(shù)據(jù)結(jié)構(gòu)的操作,棧隊列等。

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.

設(shè)計實現(xiàn)雙端隊列。
你的實現(xiàn)需要支持以下操作:

MyCircularDeque(k):構(gòu)造函數(shù),雙端隊列的大小為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 中數(shù)組中的 API 可快速實現(xiàn)一個雙向隊列。如:

arr.pop() : 刪除數(shù)組尾部最后一個數(shù)據(jù)。

arr.push() :在數(shù)組尾部插入一個數(shù)據(jù)。

arr.shift():數(shù)組頭部刪除第一個數(shù)據(jù)。

arr.unshift():數(shù)組頭部插入一個數(shù)據(jù)。

以上數(shù)組提供的 API 使得更方便的對數(shù)組進(jìn)行操作和模擬其他數(shù)據(jù)結(jié)構(gòu)的操作,棧、隊列等。

▉ 代碼實現(xiàn)
 //雙端列表構(gòu)造
        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}
        * 功能:獲取隊列頭部第一個數(shù)據(jù)
        */
        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}
        * 功能:獲取隊列尾部第一個數(shù)據(jù)
        */
        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("-----------------------------插入數(shù)據(jù)------------------------")
        console.log(`${param_1}${param_2}`)
        var param_3 = obj.deleteFront()
        var param_4 = obj.deleteLast()
        console.log("-----------------------------刪除數(shù)據(jù)------------------------")
        console.log(`${param_3}${param_4}`)
        var param_5 = obj.getFront()
        var param_6 = obj.getRear()
        console.log("-----------------------------獲取數(shù)據(jù)------------------------")
        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...
歡迎關(guān)注我個人公眾號:「一個不甘平凡的碼農(nóng)」,記錄了自己一路自學(xué)編程的故事。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/103589.html

相關(guān)文章

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

    摘要:你只可以看到在滑動窗口內(nèi)的數(shù)字?;瑒哟翱诿看沃幌蛴乙苿右晃?。返回滑動窗口最大值。算法思路暴力破解法用兩個指針,分別指向窗口的起始位置和終止位置,然后遍歷窗口中的數(shù)據(jù),求出最大值向前移動兩個指針,然后操作,直到遍歷數(shù)據(jù)完成位置。 Time:2019/4/16Title: Sliding Window MaximumDifficulty: DifficultyAuthor: 小鹿 題目...

    spacewander 評論0 收藏0
  • LeetCode JavaScript 解答104 —— 二叉樹的最大深度

    摘要:小鹿題目二叉樹的最大深度給定一個二叉樹,找出其最大深度。二叉樹的深度為根節(jié)點到最遠(yuǎn)葉子節(jié)點的最長路徑上的節(jié)點數(shù)。求二叉樹的深度,必然要用到遞歸來解決。分別遞歸左右子樹。 Time:2019/4/22Title: Maximum Depth of Binary TreeDifficulty: MediumAuthor:小鹿 題目:Maximum Depth of Binary Tre...

    boredream 評論0 收藏0
  • LeetCode JavaScript 解答226 —— 翻轉(zhuǎn)二叉樹(Invert Bina

    摘要:算法思路判斷樹是否為空同時也是終止條件。分別對左右子樹進(jìn)行遞歸。代碼實現(xiàn)判斷當(dāng)前樹是否為左右子樹結(jié)點交換分別對左右子樹進(jìn)行遞歸返回樹的根節(jié)點歡迎一起加入到開源倉庫,可以向提交您其他語言的代碼。 Time:2019/4/21Title: Invert Binary TreeDifficulty: EasyAuthor: 小鹿 題目:Invert Binary Tree(反轉(zhuǎn)二叉樹) ...

    MingjunYang 評論0 收藏0

發(fā)表評論

0條評論

Freeman

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<