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

Day10 >> 232、用栈实现队列 + 225、用队列实现栈 + 20、有效的括号

代码随想录-栈

232、用栈实现队列

没有算法逻辑,就是考察对栈这个数据结构的操作,需要多加练习

class MyQueue { Stack<Integer> stackIn; Stack<Integer> stackOut; public MyQueue() { stackIn = new Stack<>(); stackOut = new Stack<>(); } public void push(int x) { stackIn.push(x); } public int pop() { dumpstackIn(); return stackOut.pop(); } public int peek() { dumpstackIn(); int result = stackOut.pop(); stackOut.push(result); return result; } public boolean empty() { if (stackIn.isEmpty() && stackOut.isEmpty()) { return true; }else { return false; } } private void dumpstackIn() { if (!stackOut.isEmpty()) return; while (!stackIn.isEmpty()) { stackOut.push(stackIn.pop()); } } } /** * 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(); */

225、用队列实现栈

这道题也是在考察对栈和队列的掌握情况,要注意队列的操作,不要与栈搞混了,需要多加练习。

class MyStack { Queue<Integer> queue; public MyStack() { queue = new LinkedList<>(); } public void push(int x) { queue.add(x); } public int pop() { revse(); return queue.poll(); } public int top() { revse(); int result = queue.poll(); queue.offer(result); return result; } public boolean empty() { return queue.isEmpty(); } private void revse() { int size = queue.size(); size--; while (size-- > 0) { queue.add(queue.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(); */

20、有效的括号

这道题主要考察栈的数据结构特性,适用于解决有秩序要求、严格对称的一类匹配问题,重点要考虑清楚所有的不匹配场景,然后去有针对性的写处理代码,这样就很清晰,不容易乱。

class Solution { public boolean isValid(String s) { Stack<Character> st = new Stack<>(); for (char c : s.toCharArray()) { if (c == ')' && !st.isEmpty() && st.peek() == '(') { st.pop(); } else if (c == '}' && !st.isEmpty() && st.peek() == '{') { st.pop(); } else if (c == ']' && !st.isEmpty() && st.peek() == '[') { st.pop(); } else { st.push(c); } } return st.isEmpty(); } }

1047、删除字符串种的所有相邻重复项

这道题留着明天练习下

http://www.cnnetsun.cn/news/28197.html

相关文章:

  • 10、BPF 工具使用指南与技巧
  • 43、保障Web与文件服务安全:技术、挑战与应对策略
  • 47、安全文件服务配置指南
  • 49、Linux文件共享与日志管理全解析
  • 52、系统日志管理与监控全解析
  • 54、系统日志管理、监控与入侵检测技术详解
  • 强力解锁游戏控制器兼容性:ViGEmBus虚拟驱动深度指南
  • UE5 材质-30-各种节点:clamp 节点,及结合 TextureCoordinate 做出来的纹理圆效果。处理小数的数学节点 Ceil,Round,Floor,Frac
  • 智谱AI开源GLM-4-9B-Chat-1M:突破200万中文字符上下文壁垒,多模态能力引领行业新标杆
  • Windows右键菜单终极优化指南:5个技巧让系统飞起来
  • 2025年12月最新降低知网AI率的攻略,3h手把AI率降低到3%!
  • 知网AIGC检测原理是什么?如何去除知网AI痕迹?
  • 论文AI痕迹太重怎么办?6个技巧降低AI率!
  • 大模型突破:DeepSeek-OCR掀起视觉记忆革命,重新定义AI信息处理范式
  • LeetCode 448 - 找到所有数组中消失的数字
  • 22、高级系统管理与故障排除技巧
  • 第十章 for循环
  • WebRTC 是什么?能做什么?(概览篇)
  • Dubbo学习(三):深入 Remoting
  • AI设计新突破:QWEN溶图LoRA模型助力品牌视觉创作升级
  • 突破实时视频生成瓶颈:Krea Realtime 14B模型革新文本到视频技术
  • 【项目实战】Vercel 是一个让你的网站“瞬间上线”的云平台。Vercel 现在确实是技术圈的“当红炸子鸡”,尤其是在个人博客和前端开发领域。
  • Day28~实现strlen、strcpy、strncpy、strcat、strncat
  • 空洞骑士模组管理大师课:5个关键技巧让Scarab成为你的游戏管家
  • 实用方法:轻松实现NCM文件格式转换的完整解析
  • C++课后习题训练记录Day49
  • LeetCode 189. 旋转数组 | 三步反转最优解全拆解
  • downkyi视频下载:告别卡顿与画质损失的终极解决方案
  • 教你如何玩转DPDK开发中的KNI与内核交互,让网络速度翻倍!
  • Openresty驱动下的高性能Web网关实战