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

网站不同北京移动端网站

网站不同,北京移动端网站,joomla 网站模版,深圳网站建设公司哪个🌟坚持每日刷算法,😃将其变为习惯🤛让我们一起坚持吧💪 文章目录 [232. 用栈实现队列](https://leetcode.cn/problems/implement-queue-using-stacks/)思路CodeJavaC 复杂度 [225. 用队列实现栈](https://leetcode.cn/…

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

文章目录

  • [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.yayakq.cn/news/864668/

相关文章:

  • 免费app做logo的网站免费永久个人云服务器
  • 汕尾建设网站首页坂田网站建设
  • 怎么建设一个网站赚钱怎么把自己做的网站放到百度上
  • 都安网站建设怎么接做网站的任务
  • 做网站的原型 免费滁州seo公司
  • 深圳建设局网站投诉电话代理注册公司要多少钱
  • 做淘客网站哪个cms好国网商城
  • 凡客建设网站稳定吗企业网站建设一般要素包括
  • 网站设计公司域名服务器建设wordpress 试听
  • 优购物官方网站购物上海青浦区网站建设公司
  • 大同网站建设优化推广西安市在建工程项目
  • 鲜花网网站开发的意义抖音短剧推广怎么做
  • 外贸网站建站要多少钱做数学题赚钱的网站
  • 网站建设属于行政那个模块做调查的有哪些网站
  • 中国建设部门官方网站网站动态静态
  • 最火的网站开发语言安远县城乡规划建设局网站
  • 做视频上传多少个网站服务公司取名最好的名字
  • 做笔记的网站源码wordpress新版编辑
  • 2014最新网站模板-网页模板免费下载-风格吧无人在线观看高清视频8
  • 苏州网站建设搜王道下拉网站的图片怎么制作
  • 如何在国外推广自己的网站wordpress 幻燈片 插件
  • 淘客网站+wordpress免费自学编程的app
  • 精品课程网站开发的开题报告网站自创
  • 备案网站建设方案网站的出站连接数
  • 门户网站的建设和管理情况自查具有品牌的做pc端网站
  • 网站标签是什么高端建站神器
  • 台州市建设监理协会网站个人网页设计html论文
  • 新加坡网站制作擅自给公司做网站有什么责任
  • 网站备案贵州电话wordpress文章显示时间
  • 医疗网站建设基本流程图网站建设设计制作公司