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

栈与队列part01(二)

栈与队列以底层容器完成所有的工作,对外提供统一接口,且底层容器是可插拔的

1. 用栈实现队列

    // 用两个栈实现队列// in入栈// out出栈 实现FIFOstack<int> stin;stack<int> stout;MyQueue() {}void push(int x) {stin.push(x);}int pop() {// 将元素放入out准备出栈if (stout.empty()) {while (!stin.empty()) {int a = stin.top();stin.pop();stout.push(a);}   } int b = stout.top();stout.pop();return b;}int peek() {// if (!stout.empty()) return stout.top();while (!stin.empty()) {int a = stin.top();stin.pop();stout.push(a);}return stout.top();}bool empty() {if (stin.empty() && stout.empty()) return true;return false;}

2. 用队列实现栈

可以用两个队列实现栈,也可以通过改变队列中元素的排列用一个队列实现栈来提高空间利用,以下是用一个队列实现栈的方法

    // 使用一个队列实现栈需要记录队列中元素的数量// 以此知道栈顶元素的位置int count = 0;queue<int> q;MyStack() {}// 用一个队列实现栈void push(int x) { q.push(x);count++;}int pop() {// 队列不为空if (count > 0) {int a = count-1;while (a--) {int b = q.front();q.pop();q.push(b);}}int res = q.front();q.pop();count--;return res;}int top() {// 直接调用pop()获取栈顶元素,记得还要将元素入栈,元素数量+1int res = this->pop();q.push(res);count++;return res;}bool empty() {if (count == 0) return true;return false;}

3. 有效的括号

这是一个对栈的应用:

    1. 遍历字符串遇到左括号就将对应右括号放入栈中,遇到匹配的右括号就将栈顶元素出栈,如果遍历完字符串刚好栈为空,说明是有效字符串

    2.如果在遍历的过程中发现下一个右括号和栈顶括号不匹配说明括号闭合失败,不是有效字符串

    3. 如果在遍历过程中发现栈是空的,说明前面有单独的右括号,也不是有效字符串

        // 由题意得字符数量一定是偶数if (s.size() % 2 != 0 ) return false;stack<char> st;for (int i = 0; i < s.size(); i++) {if (s[i] == '(') st.push(')');else if (s[i] == '[') st.push(']');else if (s[i] == '{') st.push('}');// 如果没有左括号或者括号不能闭口(与栈顶元素不相同)不是有效字符串else if (st.empty() || st.top() != s[i]) return false;// else st.pop();}if (st.empty()) return true;return false;

4. 删除字符串中所有相邻重复项

本题也是对栈的应用:

遍历字符串将元素依次入栈,如果当前元素和栈顶元素执行出栈操作,且元素不入栈相当于完成了删除操作,最后栈内的元素就是答案

    string removeDuplicates(string s) {if (s.size() == 1) return s;else if(s.size() == 2) {if (s[0] == s[1]) return "";return s;}stack<char> st;// 执行删除操作for (int i = 0; i < s.size(); i++) {if (!st.empty() && st.top() == s[i]) {st.pop();continue;}st.push(s[i]);}// 收集结果string res;while(!st.empty()) {char a = st.top();st.pop();res+=a;}// 需要按照原字符串顺序输出reverse(res.begin(), res.end());return res;}

相关文章:

  • 服务架构演变过程
  • Java——集合类
  • 开闭原则 (Open/Closed Principle, OCP)
  • 如何让 Agent 有计划地进行股票数据分析?——基于 DeepSeek 的实战应用
  • ext2文件系统详讲
  • HashMap的hash方法是如何实现的
  • Vue 3.0中自定义指令
  • 【Python socket模块深度解析】网络通信的核心工具
  • 使用pm2 部署react+nextjs项目到服务器
  • 【IC_Design】跨时钟域的寄存器更新后锁存
  • RK3588 RGA 测试
  • 解决leetcode第3548题.等和矩阵分割II
  • 推测解码算法在 MTT GPU 的应用实践
  • C++23 容器推导指引中对于分配器的非推导语境(P1518R2)
  • MCP协议:AI时代的“万能插座”,如何重塑互联网技术生态?
  • 【1004. 最大连续1的个数 III】
  • Redis进阶之高可用
  • 操作系统学习笔记第1章 操作系统概述(灰灰题库
  • SAR ADC 的常见架构
  • Spring Task
  • 网站的下拉列表怎么做的/培训体系搭建
  • 怎样设置网站访问权限/百度推广人联系方式
  • wordpress 消息机制/seo优化范畴
  • vue 网站做中英文切换/互联网行业都有哪些工作
  • 简单的j网站建设方案书/网站seo是什么意思
  • 局域网内建设网站/百度推广后台登录首页