設計された両端キュー(デザイン円形のDeque) - JavaScriptをLeetCodeは、641個の質問に答えます

免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
このリンク: https://blog.csdn.net/qq_36903042/article/details/89348562

時間:2019年4月15日
タイトル:デザイン円形のDeque
難易度:ミディアム
著者:小鹿


トピック:デザイン円形のDeque

円形両端キュー(両端キュー)の実装を設計します。

あなたの実装では、以下の操作をサポートする必要があります。

  • MyCircularDeque(k):コンストラクタ、Kであることが両端キューのサイズを設定します。
  • insertFront():両端キューの先頭にある項目を追加します。操作が成功した場合はtrueを返します。
  • insertLast():両端キューの後部にある項目を追加します。操作が成功した場合はtrueを返します。
  • deleteFront():両端キューの先頭から項目を削除します。操作が成功した場合はtrueを返します。
  • deleteLast():両端キューの後部から項目を削除します。操作が成功した場合はtrueを返します。
  • getFront():両端キューからフロント項目を取得します。両端キューが空の場合、-1を返します。
  • getRear():両端キューから最後の項目を取得します。両端キューが空の場合、-1を返します。
  • isEmpty():両端キューが空であるかどうかをチェックします。
  • isFull():キューが満杯であるかどうかをチェックします。

設計と実装の両端キュー。
次の実現をサポートする必要があります。

  • MyCircularDeque(K):コンストラクター、サイズ両端キューをkれます。
  • insertFront():両端キューの先頭に要素を追加します。操作が成功した場合はtrueを返します。
  • insertLast():両端キューの末尾に要素を追加します。操作が成功した場合はtrueを返します。
  • deleteFront():両端キューからのヘッダ要素を削除します。操作が成功した場合はtrueを返します。
  • deleteLast():両端キュー尾から要素を削除します。操作が成功した場合はtrueを返します。
  • getFront():両端キューからのヘッダ要素を取得します。両端キューが空の場合、-1を返します。
  • getRear():両端キューの最後の要素を取得します。両端キューが空の場合、-1を返します。
  • isEmpty()は:両端キューが空であるか確認してください。
  • isFull():両端キューがいっぱいになっているかどうかを確認してください。

例:

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

注意:

  • すべての値は[0 1000]の範囲であろう。
  • 操作の数は[1 1000]の範囲であろう。
  • ビルトインのDequeライブラリを使用しないでください。

解決する:

▉アルゴリズムのアイデア

配列内の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}`)

オープンソースのGithubのリポジトリ、あなたは私に別の言語を提出することができ、コードをLeetCodeするために一緒に参加することを歓迎します。そして、一般的な私たちのオープンソースの小さな倉庫を向上、倉庫で一緒に小さなパートナーをパンチに付着!Githubにします。https:「普通の農家に不本意コード、」自分自身のセルフ・プログラミングの話を記録するすべての方法://github.com/luxiangqiang/JS-LeetCode私の個人番号に世間の注目を歓迎します。

おすすめ

転載: blog.csdn.net/qq_36903042/article/details/89348562