news 2026/4/16 13:33:03

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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、删除字符串种的所有相邻重复项

这道题留着明天练习下

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 12:41:10

13、基于BPF的流量控制分类器

基于BPF的流量控制分类器 1. 流量控制概述 流量控制(Traffic Control)是内核数据包调度子系统架构,它由各种机制和排队系统组成,能够决定数据包的流向和接收方式。其常见用例包括但不限于以下方面: - 对特定类型的数据包进行优先级排序 - 丢弃特定类型的数据包 - 进行…

作者头像 李华
网站建设 2026/4/16 13:30:35

15、XDP 编程:从加载到应用的全方位指南

XDP 编程:从加载到应用的全方位指南 1. 验证 XDP 程序加载效果 在加载完 XDP 程序后,需要验证程序是否按预期工作。可以在外部机器上再次执行 nmap 命令,观察端口 8000 是否不再可达: # nmap -sS 192.168.33.11 Starting Nmap 7.70 ( https://nmap.org ) at 2019-04-…

作者头像 李华
网站建设 2026/4/10 6:05:09

League Akari 智能助手:重新定义英雄联盟自动化体验

League Akari 智能助手&#xff1a;重新定义英雄联盟自动化体验 【免费下载链接】LeagueAkari ✨兴趣使然的&#xff0c;功能全面的英雄联盟工具集。支持战绩查询、自动秒选等功能。基于 LCU API。 项目地址: https://gitcode.com/gh_mirrors/le/LeagueAkari 那天晚上&a…

作者头像 李华
网站建设 2026/4/14 11:51:35

嵌入式开发外包哪家客户评价高

为什么嵌入式开发外包首选合肥奥鲲电子科技&#xff1f;客户评价高的背后原因在当今快速发展的科技时代&#xff0c;嵌入式开发已成为众多行业数字化转型的核心驱动力。无论是智能家居、工业自动化&#xff0c;还是医疗设备、交通系统&#xff0c;嵌入式系统都扮演着不可或缺的…

作者头像 李华