算法
(栈) $O(n)$
模拟堆栈,读到左括号压栈,读到右括号判断栈顶括号是否匹配
C++ 代码
class Solution {
public:
bool isValid(string s) {
int len = s.length();
vector<char> stack;
for (int i = 0; i < len; i++) {
// 左括号压栈
if (s[i] == '(' || s[i] == '[' || s[i] == '{') stack.push_back(s[i]);
else {
// 右括号出栈
if (stack.empty()) return false;
char top = stack[stack.size() - 1];
if (s[i] == ')' && top != '(')
return false;
if (s[i] == ']' && top != '[')
return false;
if (s[i] == '}' && top != '{')
return false;
stack.pop_back();
}
}
// 栈中无多余左括号
if (stack.size() > 0) return false;
return true;
}
};