当前位置: 首页 > news >正文

爱上算法:每日算法(24-2月4号)

🌟坚持每日刷算法,😃将其变为习惯🤛让我们一起坚持吧💪

文章目录

  • [232. 用栈实现队列](https://leetcode.cn/problems/implement-queue-using-stacks/)
    • 思路
    • Code
      • Java
      • C++
    • 复杂度
  • [225. 用队列实现栈](https://leetcode.cn/problems/implement-stack-using-queues/)
    • 思路
    • Code
      • C++
    • Java
    • 复杂度

232. 用栈实现队列

思路

首先应该先明确队列是先进先出,

而栈是先进后出,而如果想用栈实现队列,就可以尝试用两个栈

进栈和出栈

  • 进栈模拟入队列
  • 出栈模拟先出队列

画图如下

image-20230917183021976

Code

Java

class MyQueue {Stack<Integer> stIn;Stack<Integer> stOut;public MyQueue() {stIn = new Stack<>();stOut = new Stack<>();}public void push(int x) {stIn.push(x);}public int pop() {if(stOut.isEmpty()){while(!stIn.isEmpty()){stOut.push(stIn.pop());} }return stOut.pop();}public int peek() {int res = this.pop();stOut.push(res);return res;}public boolean empty() {return stOut.isEmpty()&&stIn.isEmpty();}
}/*** Your MyQueue object will be instantiated and called as such:* MyQueue obj = new MyQueue();* obj.push(x);* int param_2 = obj.pop();* int param_3 = obj.peek();* boolean param_4 = obj.empty();*/

C++

class MyQueue {
public:stack<int>	 stIn;stack<int> stOut;MyQueue() {}void push(int x) {stIn.push(x);}int pop() {if(stOut.empty()){while(!stIn.empty()){stOut.push(stIn.top());stIn.pop();}}int result = stOut.top();stOut.pop();return result;}int peek() {int res = this->pop();stOut.push(res);return res;}bool empty() {return stIn.empty()&&stOut.empty();}
};/*** Your MyQueue object will be instantiated and called as such:* MyQueue obj = new MyQueue();* obj.push(x);* int param_2 = obj.pop();* int param_3 = obj.peek();* bool param_4 = obj.empty();*/

复杂度

时间复杂度: push和empty为O(1), pop和peek为O(n)

空间复杂度: O(n)

225. 用队列实现栈

思路

队列是先进先出原则,

而栈是先进后出原则

因此,可以使用两个队列来实现栈

可以使用一个队列来实现栈

满足先进后出的方法就是; 入队列之后,就将这个数放到队首

image-20230917184757095

Code

C++

class MyStack {
public:	queue<int> que;MyStack() {}void push(int x) {que.push(x);}int pop() {int size = que.size();size--;while(size--){que.push(que.front());que.pop();}int res = que.front();que.pop();return res;}int top() {return que.back();}bool empty() {return que.empty();}
};/*** Your MyStack object will be instantiated and called as such:* MyStack* obj = new MyStack();* obj->push(x);* int param_2 = obj->pop();* int param_3 = obj->top();* bool param_4 = obj->empty();*/

Java

class MyStack {Queue<Integer> que = new LinkedList<>();public MyStack() {}public void push(int x) {que.add(x);}public int pop() {rePosition();return que.poll();}public int top() {rePosition();int res = que.poll();que.add(res);return res;}public boolean empty() {return que.isEmpty();}public void rePosition(){int size = que.size();size--; // 不包括刚刚添加的数while(size-- > 0){que.add(que.poll());}}
}/*** Your MyStack object will be instantiated and called as such:* MyStack obj = new MyStack();* obj.push(x);* int param_2 = obj.pop();* int param_3 = obj.top();* boolean param_4 = obj.empty();*/

复杂度

  • 时间复杂度: pop为O(n),其他为O(1)
  • 空间复杂度: O(n)
http://www.lryc.cn/news/294442.html

相关文章:

  • 【Node系列】创建第一个服务器应用
  • Linux命令基础学习 (2月4日打卡
  • Python 基础知识概览
  • Adobe Camera Raw for Mac v16.1.0中文激活版
  • zabbix自定义监控项
  • 使用Pycharm在本地调用chatgpt的接口
  • HarmonyOS远程真机调试方法
  • 基于SpringBoot的后端导出Excel文件
  • 2 月 5 日算法练习- 动态规划
  • SpringBoot整合EasyCaptcha图形验证码
  • 学习数据结构和算法的第3天
  • SpringBoot实战第三天
  • mysql学习打卡day22
  • Unity | Spine动画记录
  • 【Flink】FlinkSQL实现数据从MySQL到MySQL
  • python爬虫抓取新闻并且植入自己的mysql远程数据库内
  • netty实现简单的客户端、服务端互相发消息
  • 利用jmeter完成简单的压力测试
  • 【手写数据库toadb】toadb物理存储模型,数据库物理存储原理,物理文件组织关系以及行列混合模型存储结构
  • MySQL-----DDL基础操作
  • 【MySQL】在 Centos7 环境安装 MySQL -- 详细完整教程
  • 理解React中的setState()方法
  • 数据库管理-第144期 深入使用EMCC-01(20240204)
  • flask_django_python五金电商网络营销的可视化分析研究
  • Java并发(二十三)----同步模式之保护性暂停
  • ###C语言程序设计-----C语言学习(9)#函数基础
  • Dockerfile文件参数配置和使用
  • Java实现婚恋交友网站 JAVA+Vue+SpringBoot+MySQL
  • React16源码: React中详解在渲染阶段Suspend的源码实现
  • mac电脑风扇控制软件:Macs Fan Control Pro for mac 激活版