feat: 20_valid_parentheses

This commit is contained in:
SquidSpirit 2025-02-12 23:04:31 +08:00
parent 593382dc0a
commit 3284c1e532

View File

@ -0,0 +1,43 @@
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for (auto c : s) {
switch (c) {
case '(':
case '[':
case '{':
stk.emplace(c);
break;
case ')':
if (stk.empty() || '(' != stk.top()) {
return false;
}
stk.pop();
break;
case ']':
if (stk.empty() || '[' != stk.top()) {
return false;
}
stk.pop();
break;
case '}':
if (stk.empty() || '{' != stk.top()) {
return false;
}
stk.pop();
break;
}
}
if (!stk.empty()) {
return false;
}
return true;
}
};