class Solution {
public:
bool isValid(string s) {
stack<char> parentheses;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') parentheses.push(s[i]);
else {
if (parentheses.empty()) return false;
if (s[i] == ')' && parentheses.top() != '(') return false;
if (s[i] == ']' && parentheses.top() != '[') return false;
if (s[i] == '}' && parentheses.top() != '{') return false;
parentheses.pop();
}
}
return parentheses.empty();
}
};
相关文章
- 03-21leetcode-mid-backtracking -22. Generate Parentheses-79 Word Search -NO
- 03-2120.Valid Parentheses
- 03-21LeetCode.1021-删除最外面的括号(Remove Outermost Parentheses)
- 03-21Valid Parentheses
- 03-21F - Parentheses
- 03-21Leetcode-5016 Remove Outermost Parentheses(删除最外层的括号)
- 03-2132. Longest Valid Parentheses
- 03-2120. Valid Parentheses[E]有效的括号
- 03-2120-Valid Parentheses
- 03-21【LeetCode每天一题】Valid Parentheses(有效的括弧)