很简单,用栈就可以了…
class Solution {
public:
bool isValid(string s) {
int len = size(s);
if (len == 0)return true;
stack<char> re;
for (int i = 0; i < len; i++) {
if (re.empty()) {
re.push(s[i]);
}
else {
if ((s[i] == re.top() + 1) || (s[i] == re.top() + 2)) {
re.pop();
}
else {
re.push(s[i]);
}
}
}
return re.empty();
}
};