feat: 20_valid_parentheses
This commit is contained in:
parent
593382dc0a
commit
3284c1e532
43
20_valid_parentheses/main.cpp
Normal file
43
20_valid_parentheses/main.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user