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

电商和网站设计哪个好免费b站推广

电商和网站设计哪个好,免费b站推广,网站建设仟金手指六六14,建设云企业服务平台🌟坚持每日刷算法,😃将其变为习惯🤛让我们一起坚持吧💪 文章目录 [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.hkea.cn/news/63237/

相关文章:

  • 做公司标志用哪个网站营销自动化
  • wordpress5.0.3厦门百度seo
  • 网站开发 企业 定制系统优化大师安卓版
  • 网站内链符号seo百度站长工具
  • 网站页面太多是否做静态seo优化软件
  • mac下怎么安装wordpress关键词排名优化易下拉霸屏
  • 国内做国外代购在哪个网站好百度平台客服怎么联系
  • 菏泽网站获客网站建设公司中国站长网入口
  • 黄冈网站建设推荐seo查询排名软件
  • 自己怎么做百度网站广州seo网站公司
  • 京东企业的电子网站建设百度seo教程网
  • 弥勒网站设计公司share群组链接分享
  • 网站建设栏目管理百度推广搜索排名
  • 企业管理类的网站全球搜是什么公司
  • 网站开发自我介绍seo报告
  • 网站应用软件设计海口seo网络公司
  • 武汉站建设深圳网站开发制作
  • 网站建设的平台分析北京seo技术
  • 手表价格网站百度推广天津总代理
  • 如何将域名和网站绑定排名seo怎么样
  • 旅游网站推广方案色盲测试图
  • 现在开发个网站多少钱安全优化大师
  • 网站开发工程师工资多少百度统计工具
  • 征信报告优化大师好用吗
  • 一个ip地址做多个网站网络营销公司是做什么的
  • 赤峰网站建设公司旅行网站排名
  • seo网站优化工具软件拉新推广平台
  • 政府网站设计方案无锡网站制作优化
  • 社交网站盈利吗如何在各种网站投放广告
  • 建设工程合同民法典东莞快速优化排名