摘要:小鹿題目設(shè)計(jì)實(shí)現(xiàn)雙端隊(duì)列。你的實(shí)現(xiàn)需要支持以下操作構(gòu)造函數(shù)雙端隊(duì)列的大小為。獲得雙端隊(duì)列的最后一個(gè)元素。檢查雙端隊(duì)列是否為空。數(shù)組頭部刪除第一個(gè)數(shù)據(jù)。以上數(shù)組提供的使得更方便的對(duì)數(shù)組進(jìn)行操作和模擬其他數(shù)據(jù)結(jié)構(gòu)的操作,棧隊(duì)列等。
Time:2019/4/15
Title: Design Circular Deque
Difficulty: Medium
Author: 小鹿
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è)計(jì)實(shí)現(xiàn)雙端隊(duì)列。
你的實(shí)現(xiàn)需要支持以下操作:MyCircularDeque(k):構(gòu)造函數(shù),雙端隊(duì)列的大小為k。
insertFront():將一個(gè)元素添加到雙端隊(duì)列頭部。 如果操作成功返回 true。
insertLast():將一個(gè)元素添加到雙端隊(duì)列尾部。如果操作成功返回 true。
deleteFront():從雙端隊(duì)列頭部刪除一個(gè)元素。 如果操作成功返回 true。
deleteLast():從雙端隊(duì)列尾部刪除一個(gè)元素。如果操作成功返回 true。
getFront():從雙端隊(duì)列頭部獲得一個(gè)元素。如果雙端隊(duì)列為空,返回 -1。
getRear():獲得雙端隊(duì)列的最后一個(gè)元素。 如果雙端隊(duì)列為空,返回 -1。
isEmpty():檢查雙端隊(duì)列是否為空。
isFull():檢查雙端隊(duì)列是否滿了。
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 可快速實(shí)現(xiàn)一個(gè)雙向隊(duì)列。如:
arr.pop() : 刪除數(shù)組尾部最后一個(gè)數(shù)據(jù)。
arr.push() :在數(shù)組尾部插入一個(gè)數(shù)據(jù)。
arr.shift():數(shù)組頭部刪除第一個(gè)數(shù)據(jù)。
arr.unshift():數(shù)組頭部插入一個(gè)數(shù)據(jù)。
以上數(shù)組提供的 API 使得更方便的對(duì)數(shù)組進(jìn)行操作和模擬其他數(shù)據(jù)結(jié)構(gòu)的操作,棧、隊(duì)列等。
//雙端列表構(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} * 功能:隊(duì)列頭部入隊(duì) */ 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} * 功能:隊(duì)列尾部入隊(duì) */ 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} * 功能:隊(duì)列頭部出隊(duì) */ 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} * 功能:隊(duì)列尾部出隊(duì) */ 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} * 功能:獲取隊(duì)列頭部第一個(gè)數(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} * 功能:獲取隊(duì)列尾部第一個(gè)數(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} * 功能:判斷雙端隊(duì)列是否為空 */ 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} * 功能:判斷雙端隊(duì)列是否為滿 */ MyCircularDeque.prototype.isFull = function() { if(this.deque.length === this.size){ return true; }else{ return false; } }; //測(cè)試 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}`)
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/103589.html
摘要:你只可以看到在滑動(dòng)窗口內(nèi)的數(shù)字。滑動(dòng)窗口每次只向右移動(dòng)一位。返回滑動(dòng)窗口最大值。算法思路暴力破解法用兩個(gè)指針,分別指向窗口的起始位置和終止位置,然后遍歷窗口中的數(shù)據(jù),求出最大值向前移動(dòng)兩個(gè)指針,然后操作,直到遍歷數(shù)據(jù)完成位置。 Time:2019/4/16Title: Sliding Window MaximumDifficulty: DifficultyAuthor: 小鹿 題目...
摘要:小鹿題目二叉樹的最大深度給定一個(gè)二叉樹,找出其最大深度。二叉樹的深度為根節(jié)點(diǎn)到最遠(yuǎn)葉子節(jié)點(diǎn)的最長路徑上的節(jié)點(diǎn)數(shù)。求二叉樹的深度,必然要用到遞歸來解決。分別遞歸左右子樹。 Time:2019/4/22Title: Maximum Depth of Binary TreeDifficulty: MediumAuthor:小鹿 題目:Maximum Depth of Binary Tre...
摘要:算法思路判斷樹是否為空同時(shí)也是終止條件。分別對(duì)左右子樹進(jìn)行遞歸。代碼實(shí)現(xiàn)判斷當(dāng)前樹是否為左右子樹結(jié)點(diǎn)交換分別對(duì)左右子樹進(jìn)行遞歸返回樹的根節(jié)點(diǎn)歡迎一起加入到開源倉庫,可以向提交您其他語言的代碼。 Time:2019/4/21Title: Invert Binary TreeDifficulty: EasyAuthor: 小鹿 題目:Invert Binary Tree(反轉(zhuǎn)二叉樹) ...
閱讀 3407·2023-04-26 02:41
閱讀 2455·2023-04-26 00:14
閱讀 2862·2021-08-11 10:22
閱讀 1284·2019-12-27 11:38
閱讀 3576·2019-08-29 18:34
閱讀 2384·2019-08-29 12:13
閱讀 2955·2019-08-26 18:26
閱讀 1855·2019-08-26 16:49