「代码随想录算法训练营」第九天 | 栈与队列 part1
232. 用栈实现队列
题目链接:https://leetcode.cn/problems/implement-queue-using-stacks/
题目难度:简单
文章讲解:https://programmercarl.com/0232.用栈实现队列.html
视频讲解:https://www.bilibili.com/video/BV1nY4y1w7VC
题目状态:看视频讲解后通过
思路:
通过两个栈来实现队列。
- 创建两个栈
stackIn
和stackOut
; - 当入队的时候,直接把元素入
stackIn
栈中即可; - 当出队的时候,若
stackOut
中没有元素时,先将stackIn
中的元素入stackOut
中,再从stackOut
中出栈; - 当返回队头时,直接调用出队的代码即可;
- 当判断是否为空队的时候,只需要判断两个栈是否都为空即可。
代码实现:
class MyQueue { public: stack<int> stackIn; stack<int> stackOut; MyQueue() { } void push(int x) { stackIn.push(x); } int pop() { if(stackOut.empty()) { while(!stackIn.empty()) { stackOut.push(stackIn.top()); stackIn.pop(); } } int res = stackOut.top(); stackOut.pop(); return res; } int peek() { int res = this->pop(); stackOut.push(res); return res; } bool empty() { return stackIn.empty() && stackOut.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(); */
225. 用队列实现栈
题目链接:https://leetcode.cn/problems/implement-stack-using-queues/
题目难度:简单
文章讲解:https://programmercarl.com/0225.用队列实现栈.html
视频讲解:https://www.bilibili.com/video/BV1Fd4y1K7sm
题目状态:看代码随想录思路后通过
思路一(两个队列):
- 创建两个队列,一个是用来各种操作的队列
que1
,一个用来存储元素的辅助队列que2
; - 入栈:将元素压入
que1
中即可; - 出栈:将
que1
中除了最后一个元素以外的其他元素压入que2
中,并将que1
中的最后一个元素输出,最后将que2
中的元素再返给que1
并清空que2
; - 输出栈头:返回
que1
的队尾; - 判断栈是否为空:返回
que1
队列是否为空。
代码实现:
class MyStack { public: queue<int> que1; queue<int> que2; MyStack() { } void push(int x) { que1.push(x); } int pop() { int size = que1.size(); size--; while(size--) { que2.push(que1.front()); que1.pop(); } int res = que1.front(); que1.pop(); que1 = que2; while(!que2.empty()) { que2.pop(); } return res; } int top() { return que1.back(); } bool empty() { return que1.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(); */
思路二(一个队列)
- 创建一个队列
que
; - 入栈:将元素压入
que
中即可; - 出栈:将
que
中除了最后一个元素以外的其他元素重新压入que
中,并将que
刚开始的最后一个元素(现在是第一个元素)弹出; - 输出栈头:返回
que
的队尾; - 判断栈是否为空:返回
que
队列是否为空。
代码实现:
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(); */
20. 有效的括号
题目链接:https://leetcode.cn/problems/valid-parentheses/
题目难度:简单
文章讲解:https://programmercarl.com/0020.有效的括号.html
视频讲解:https://www.bilibili.com/video/BV1AF411w78g
题目状态:看代码随想录思路后通过
思路:
- 若字符串中元素的个数为奇数,说明括号肯定不匹配,直接返回
false
; - 创建一个栈;
- 如果遍历的元素是括号的左半部分,将对应的右半部分压入到栈中;
- 如果遍历到的元素是括号的右半部分,则判断栈中的元素是否和该括号对应,若不对应,说明两个括号无法匹配,则返回
false
; - 若栈中元素和遍历到的元素匹配且最终栈为空,则返回
true
。
代码实现:
class Solution { public: bool isValid(string s) { if(s.size() % 2 != 0) return false; stack<char> st; for(auto &sElem : s) { if(sElem == '(') st.push(')'); else if(sElem == '{') st.push('}'); else if(sElem == '[') st.push(']'); else if(st.empty() || st.top() != sElem) return false; else st.pop(); } return st.empty(); } };
1047. 删除字符串中的所有相邻重复项
题目链接:https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
题目难度:简单
文章讲解:https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html
视频讲解:https://www.bilibili.com/video/BV12a411P7mw
题目状态:通过
个人思路:
- 创建一个栈来存放字符串中的元素;
- 遍历字符串;
- 若栈不为空且遍历到的字符串中的元素等于栈中的元素,将栈中元素弹出;
- 相反,若遍历到的元素不等于栈中的元素,则将元素压入栈中;
- 遍历完成后,将栈中的元素依次弹出,并存储在一个
string
类型的变量res
中; - 反转
res
并返回。
代码实现:
class Solution { public: string removeDuplicates(string s) { int size = s.size(); stack<char> st; for(auto &sElem : s) { if(!st.empty() && sElem == st.top()) st.pop(); else st.push(sElem); } string res; while(!st.empty()) { res += st.top(); st.pop(); } reverse(res.begin(), res.end()); return res; } };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?