-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.cpp
37 lines (37 loc) · 883 Bytes
/
20.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
class Solution
{
public:
bool isValid(string s)
{
stack<char> tmp;
for (char ch : s)
{
if (ch == '(' || ch == '[' || ch == '{')
tmp.push(ch);
else
{
if (tmp.empty())
return false;
if (ch == ')')
{
if (tmp.top() != '(')
return false;
}
else if (ch == ']')
{
if (tmp.top() != '[')
return false;
}
else if (ch == '}')
{
if (tmp.top() != '{')
return false;
}
tmp.pop();
}
}
if (!tmp.empty())
return false;
return true;
}
};