### 20 ```C++ class Solution { public: stack<char> In; stack<char> Out; bool isValid(string s) { for(int i=0;i<s.length();i++){ if(s[i]=='(') In.push(')'); else if(s[i]=='[') In.push(']'); else if(s[i]=='{') In.push('}'); else if(In.empty()||In.top()!=s[i]) return false; else In.pop(); } return In.empty(); } }; ``` ### 1047 ```C++ class Solution { public: string removeDuplicates(string s) { stack<char> st; for(int i=0;i<s.length();i++){ if(!st.empty()&&st.top()==s[i]){ st.pop(); }else{ st.push(s[i]); } } string res; while(!st.empty()){ res+=st.top(); st.pop(); } reverse(res.begin(),res.end()); return res; } }; ``` ### 150 ```C++ class Solution { public: int evalRPN(vector<string>& tokens) { stack<long long> st; long long res; for(int i=0;i<tokens.size();i++){ if(tokens[i]=="+"||tokens[i]=="-"||tokens[i]=="*"||tokens[i]=="/"){ long long temp1=st.top(); st.pop(); long long temp2=st.top(); st.pop(); if(tokens[i]=="+"){ res=temp2+temp1; st.push(res); } if(tokens[i]=="-"){ res=temp2-temp1; st.push(res); } if(tokens[i]=="/"){ res=temp2/temp1; st.push(res); } if(tokens[i]=="*"){ res=temp2*temp1; st.push(res); } }else{ st.push(stoll(tokens[i])); } } long long result=st.top(); st.pop(); return result; } }; ```
代码随想录day11
最新推荐文章于 2024-09-12 19:02:20 发布