-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalid-parentheses.cpp
More file actions
44 lines (38 loc) · 1.12 KB
/
Copy pathvalid-parentheses.cpp
File metadata and controls
44 lines (38 loc) · 1.12 KB
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
class Solution {
public:
bool isValid(string s) {
stack<char> pars;
for ( auto it = s.begin(); it != s.end(); it++ ) {
if ( *it == '[' || *it == '(' || *it == '{') {
pars.push(*it);
continue;
}
if ( *it == ']' ) {
if ( pars.empty() ) return false;
if ( pars.top() != '[' )
return false;
else {
pars.pop();
}
}
if ( *it == ')' ) {
if ( pars.empty() ) return false;
if ( pars.top() != '(' )
return false;
else {
pars.pop();
}
}
if ( *it == '}' ) {
if ( pars.empty() ) return false;
if ( pars.top() != '{' )
return false;
else {
pars.pop();
}
}
}
if ( !pars.empty() ) return false;
return true;
}
};