优先算法专题十二——栈
1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)
注意空的删不了字符即可,处理一下
class Solution {
public:string removeDuplicates(string s) {// stack<char> st;// for(auto& ch : s)// {// if(st.size() && ch == st.top()) st.pop();// else st.push(ch);// }// string ans;// while(!st.empty())// {// ans += st.top();// st.pop();// }// reverse(ans.begin(), ans.end());// return ans;//不使用栈数据结构string ans;for(int i = 0; i < s.size(); i++){if(ans.size() && ans.back() == s[i]) ans.pop_back();else ans += s[i];}return ans;}
};
844. 比较含退格的字符串 - 力扣(LeetCode)
和上题类似,但是要注意多个退格的情况,此时直接跳过即可
class Solution {
public:bool backspaceCompare(string s, string t) {string S, T;for(auto& ch : s){if(ch == '#' && S.size()) S.pop_back();else if(ch == '#' && !S.size());else S += ch;}for(auto& ch : t){if(ch == '#' && T.size()) T.pop_back();else if(ch == '#' && !T.size());else T += ch;}return T == S;}
};
227. 基本计算器 II - 力扣(LeetCode)
//当一个数字前面符号为'+',将这个数字入栈;为'-'将这个数字相反数入栈;为'*'或者'/'将栈顶数据和这个数据运算结果入栈
//最终让栈里面数据进行加法即可
class Solution
{
public:int calculate(string s) {char op = '+';//初始化为'+'正确将第一个数据入栈(数据都是正数)stack<int> st;int num = 0;//遍历到的数字int p = 0;while(p < s.size()){if(s[p] == ' ') {p++;}else if(s[p] == '+' || s[p] == '-' || s[p] == '*' || s[p] == '/') {op = s[p];//更新数据前面的操作码p++;}else//处理数据{int tmp = s[p] - '0';while(0 <= tmp && tmp <= 9){num = tmp + num * 10;p++;tmp = s[p] - '0';}if(op == '+') st.push(num);else if(op == '-') st.push(-1 * num);else{int top = st.top();st.pop();if(op == '*') st.push(top * num);else st.push(top / num);}num = 0;//将数字置0,避免影响下次数字计算}} int ans = 0;while(st.size()){ans += st.top();st.pop();} return ans;}
};
394. 字符串解码 - 力扣(LeetCode)
//使用三个栈
//一个栈存数字,一个栈存'[',一个栈存字符串
//当括号匹配时,拿出数字和字符串构成字符串
//当存放'['的栈和存放字符串的栈的元素个数相同时,说明此时的字符串是在和栈顶字符串的同一层[]内,那么将栈顶元素拿出和此时的字符串构成新的字符串入栈即可;当个数不同时,说明此时字符串在另一层[]中,直接入栈即可;当存放'['的栈为空时,说明此时的字符串在最外层,直接加到结果上面即可
//不仅仅是新生成的字符串需要这样,当']'匹配时构建的新的重复字符串特要这样
//总而言之就是将同层的字符串拼接在一起
class Solution {
public:string decodeString(string s) {stack<int> s1;stack<char> s2;stack<string> s3;string ans;int i = 0, n = s.size();while (i < n) {if(s[i] >= '0' && s[i] <= '9'){int num = 0, tmp = 0;while(s[i] >= '0' && s[i] <= '9'){tmp = s[i] - '0';num = tmp + num * 10;i++;}s1.push(num);}else if(s[i] == '['){s2.push('[');i++;}else if(s[i] >= 'a' && s[i] <= 'z'){string str;while (s[i] >= 'a' && s[i] <= 'z'){str += s[i];i++;}if(s2.empty()) ans += str;else if(s2.size() != s3.size()) s3.push(str); else if(s2.size() == s3.size()){string tmp = s3.top();s3.pop();s3.push(tmp + str);}}else//这个就是']'括号匹配{s2.pop();int num = s1.top(); s1.pop();string str = s3.top(); s3.pop();string str2;for(int j = 0; j < num; j++) str2 += str;if(s2.empty()){ans += str2;}else if(s2.size() != s3.size()) {s3.push(str2);}else if(s2.size() == s3.size()){string tmp = s3.top();s3.pop();s3.push(tmp + str2);}i++;}}while(!s3.empty()) {ans += s3.top();s3.pop();}return ans;}
};
946. 验证栈序列 - 力扣(LeetCode)
//用一个栈模拟push和pop的过程:
//遍历pushed入栈,先入栈,当入栈元素和poped中元素相同时出栈
//当栈中还存在元素返回false,否则返回true
class Solution
{
public:bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {stack<int> st;int i = 0, j = 0;int n = pushed.size(), m = popped.size();while(i < n){st.push(pushed[i++]);while(j < m && st.size() && st.top() == popped[j]){st.pop();j++;}}if(st.size()) return false;else return true;}
};