首页 > 编程语言 >代码随想录算法训练营第十天 | ● 理论基础 ● 232.用栈实现队列 ● 225. 用队列实现栈

代码随想录算法训练营第十天 | ● 理论基础 ● 232.用栈实现队列 ● 225. 用队列实现栈

时间:2023-11-19 21:14:07浏览次数:30  
标签:function return 第十天 队列 stackIn 随想录 length var stackOut

今日学习的文章链接和视频链接

● 232.用栈实现队列


var MyQueue = function() {
    this.stackIn = [];
    this.stackOut = []

};

/** 
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function(x) {
    this.stackIn.push(x)
};

/**
 * @return {number}
 */
MyQueue.prototype.pop = function() {
    if(this.stackOut.length){
        return this.stackOut.pop()
    }
    console.log(this)
    while(this.stackIn.length){
        this.stackOut.push(this.stackIn.pop())
    }
    // console.log(this)
    return this.stackOut.pop()
};

/**
 * @return {number}
 */
MyQueue.prototype.peek = function() {
    console.log(this)
    let temp = this.pop();
    // console.log(temp)
    this.stackOut.push(temp);
    return temp
};

/**
 * @return {boolean}
 */
MyQueue.prototype.empty = function() {
    // console.log(this.stackIn.length,this.stackOut.length)
    return !this.stackIn.length&&!this.stackOut.length

};

/**
 * Your MyQueue object will be instantiated and called as such:
 * var obj = new MyQueue()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.empty()
 */

● 225. 用队列实现栈


var MyStack = function() {
    this.stackIn = [];
    this.stackOut = [];

};

/** 
 * @param {number} x
 * @return {void}
 */
MyStack.prototype.push = function(x) {
    this.stackIn.push(x)
};

/**
 * @return {number}
 */
MyStack.prototype.pop = function() {
    if(!this.stackIn.length){
        [this.stackIn,this.stackOut] = [this.stackOut , this.stackIn];
    }
    while(this.stackIn.length>1){
        this.stackOut.push(this.stackIn.shift());
    }
    return this.stackIn.shift()

};

/**
 * @return {number}
 */
MyStack.prototype.top = function() {
    let temp = this.pop();
    this.stackOut.push(temp);
    return temp


};

/**
 * @return {boolean}
 */
MyStack.prototype.empty = function() {
    return !this.stackIn.length&&!this.stackOut.length

};

/**
 * Your MyStack object will be instantiated and called as such:
 * var obj = new MyStack()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.empty()
 */

今日收获,记录一下自己的学习时长

1h

标签:function,return,第十天,队列,stackIn,随想录,length,var,stackOut
From: https://www.cnblogs.com/lijiacheng/p/17842630.html

相关文章

  • 代码随想录算法训练营第十天 | ●28. 实现 strStr() ●459.重复的子字符串 ●字符串
    今日学习的文章链接和视频链接https://programmercarl.com/字符串总结.html#什么是字符串https://programmercarl.com/双指针总结.html#数组篇●28.实现strStr()varstrStr=function(haystack,needle){if(needle.length===0)return0;const......
  • 代码随想录算法训练营第八天 | ● 344.反转字符串 ● 541. 反转字符串II ● 卡码网:54
    今日学习内容●344.反转字符串varreverseString=function(s){//returns.reverse();for(leti=0,j=s.length-1;i<Math.floor(s.length/2);i++,j--){[s[i],s[j]]=[s[j],s[i]]}returns};●541.反转字符串IIvarre......
  • 代码随想录算法训练营第七天 | ● 454.四数相加II ● 383. 赎金信 ● 15. 三数之和
    今日学习的文章链接和视频链接https://programmercarl.com/链表理论基础.html●454.四数相加IIvarfourSumCount=function(nums1,nums2,nums3,nums4){letcount=0letmap=newMap();for(letnumber1ofnums1){for(letnumber2ofnums......
  • 【HDU 1276】士兵队列训练问题 题解(链表+模拟)
    某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至......
  • 代码随想录算法训练营第六天 |● 哈希表理论基础 ● 242.有效的字母异位词 ● 349.
    今日学习的文章链接和视频链接https://programmercarl.com/哈希表理论基础.html242.有效的字母异位词varisAnagram=function(s,t){if(s.length!==t.length)returnfalseletmap=newMap();for(letcharofs){if(!map.get(char)){......
  • 队列
    队列队列(queue)是一种具有「先进入队列的元素一定先出队列」性质的表。由于该性质,队列通常也被称为先进先出(firstinfirstout)表,简称FIFO表。STL队列​ 以下操作的复杂度均为\(O(1)\)。创建队列queue<int>qqueue<char>qqueue<string>q元素访问q.front()返......
  • activemq 配置延时队列
    conf/activemq.xml新增配置<brokerxmlns="http://activemq.apache.org/schema/core"brokerName="localhost"dataDirectory="${activemq.data}"schedulerSupport="true">jmsMessagingTemplate.convertAndSend(QUEUE,newH......
  • setTimeout可以将字符串当成代码执行,类比eval函数。当遇到setTimeout或者SetInterval,
    请问以下JS代码的输出顺序是?letdate=newDate()setTimeout(()=>{console.log('1')},2000)setTimeout('console.log(2)',1000);setTimeout(function(){console.log('3')},1500);while((newDate()-date)<3000){}A报错B......
  • 队列
    #include<stdio.h>#include<stdlib.h>//队列结点的定义typedefstructQNode{intdata;structQNode*next;}QNode;//链式队列的定义typedefstruct{QNode*front;//队头指针QNode*rear;//队尾指针}LinkedQueue;//初始化链式队列......
  • 循环队列
    一、普通队列(顺序存储结构)说明:rear指向队尾元素,front指向对头元素的下一个元素。i.判断元素个数:number=rear-front;ii.判断队空:rear==frontiii.插入元素:rear++;iiii.删除元素:front++;iiiii.队满操作:rear==length-1;2.2遇到假溢出问题如严蔚敏老师数据结构书中,写道,每次插......