class Solution {
public:
bool isValid(string s) {
stack<char> res;
if(s.size() % 2 != 0) return false;
//用ASCLL码简单便捷!!!
for(int i = 0;i < s.size();i++)
{
if(s[i] == '(' || s[i] == '['|| s[i] == '{') res.push(s[i]);
else
{
if(res.size() && abs(res.top() - s[i]) <= 2) res.pop();
else return false;
}
}
return res.empty(); //返回方式还需多加思索
}
};
class Solution {
public:
string removeDuplicates(string s) {
stack<char> st;
for(int i = 0;i < s.size();i++)
{
if(st.empty() || s[i] != st.top()) st.push(s[i]);
else st.pop();
}
string str = "";
while(!st.empty())
{
str += st.top();
st.pop();
}
reverse(str.begin(),str.end());
return str;
}
};
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for(auto c: tokens)
{
if(c == "+" || c == "-" || c == "*" || c == "/" )
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
if(c == "+") st.push(a + b);
else if(c == "-") st.push(b - a);
else if(c == "*") st.push(long(a)*long(b)); //强制转换
else if(c == "/") st.push(b/a);
}
else{
st.push(stol(c));
}
}
return st.top();
}
};