随笔分类 -  栈和队列

摘要:思路: 当字符串为运算符号是弹出栈中两个数字进行运算 stoi("1") 将string转换为int class Solution { public: int evalRPN(vector<string>& tokens) { stack<int> stack; for (int i = 0; i 阅读全文
posted @ 2022-08-27 09:59 hjy94wo 阅读(16) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: string removeDuplicates(string s) { stack<char> stack; for (int i = 0; i < s.size(); i ++) { if (stack.empty() || s[i] != sta 阅读全文
posted @ 2022-08-27 09:18 hjy94wo 阅读(14) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: bool isValid(string s) { stack<char> stack; for (int i = 0; i < s.size(); i ++) { if (s[i] == ')' || s[i] =='}' || s[i] == '] 阅读全文
posted @ 2022-08-27 09:10 hjy94wo 阅读(11) 评论(0) 推荐(0) 编辑
摘要:思路: 一个队列用于备份,先将原队列最后一位元素之前的元素弹出,push进备份队列,后再还原 class MyStack { public: queue<int> queue1; queue<int> queue2; //备份 MyStack() { } void push(int x) { que 阅读全文
posted @ 2022-08-26 19:22 hjy94wo 阅读(16) 评论(0) 推荐(0) 编辑
摘要:思路: 用两个栈实现队列 pop操作,若out栈为空则先将in中元素push进out,再pop出out中元素 peek操作,直接调用pop,在将pop出元素push进out class MyQueue { public: stack<int> in; stack<int> out; MyQueue( 阅读全文
posted @ 2022-08-26 19:10 hjy94wo 阅读(19) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示