-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path020_Valid_Parentheses.cpp
57 lines (56 loc) · 1.26 KB
/
020_Valid_Parentheses.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<stack>
#include<vector>
#include<string>
#include<map>
using namespace std;
static const auto x=[](){
std::ios::sync_with_stdio(false);
std:cin.tie(nullptr);
return nullptr;
}();
class Solution
{
public:
bool isValid(string s)
{
stack<char> mystack;
map<char,char> expect = {
{'(', ')'},
{'[', ']'},
{'{', '}'}
};
while(not s.empty())
{
char a = s[0];
if(a == '(' || a == '{' || a == '[' )
mystack.push(a);
else
{
if( mystack.empty() )
return false;
else
{
char top = mystack.top();
char expect_char = expect[top];
if( a == expect_char)
mystack.pop();
else
return false;
}
}
s = s.substr(1);
}
if( mystack.empty() )
return true;
else
return false;
}
};
int main(int argc, char const *argv[])
{
Solution sol;
string s("{[]}");
cout<<sol.isValid(s);
return 0;
}