随笔分类 - 栈和队列
摘要:思路: 当字符串为运算符号是弹出栈中两个数字进行运算 stoi("1") 将string转换为int class Solution { public: int evalRPN(vector<string>& tokens) { stack<int> stack; for (int i = 0; i
阅读全文
摘要:class Solution { public: string removeDuplicates(string s) { stack<char> stack; for (int i = 0; i < s.size(); i ++) { if (stack.empty() || s[i] != sta
阅读全文
摘要: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] == ']
阅读全文
摘要:思路: 一个队列用于备份,先将原队列最后一位元素之前的元素弹出,push进备份队列,后再还原 class MyStack { public: queue<int> queue1; queue<int> queue2; //备份 MyStack() { } void push(int x) { que
阅读全文
摘要:思路: 用两个栈实现队列 pop操作,若out栈为空则先将in中元素push进out,再pop出out中元素 peek操作,直接调用pop,在将pop出元素push进out class MyQueue { public: stack<int> in; stack<int> out; MyQueue(
阅读全文