-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcodeValidParentheses.cpp
More file actions
58 lines (52 loc) · 1.43 KB
/
Copy pathLeetcodeValidParentheses.cpp
File metadata and controls
58 lines (52 loc) · 1.43 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
public:
bool isValid(string s) {
if(s.size()%2!=0)
return false;
stack<char>lefts;
for(int i = 0; i<s.size(); i++)
{
if(s.at(i)=='('||s.at(i)=='['||s.at(i)=='{')
{
lefts.push(s.at(i));
}
else
{
if(lefts.empty()==true)
{
//cout<<"L18";
return false;
}
else
{
switch(lefts.top())
{
case '(':
//cout<<"L26";
if(s.at(i)!=')')
return false;
break;
case '[':
//cout<<"L31";
if(s.at(i)!=']')
return false;
break;
case '{':
//cout<<"L36";
if(s.at(i)!='}')
return false;
break;
}
lefts.pop();
}
}
}
if(lefts.empty()==false)
{
//cout<<"L48";
return false;
}
//cout<<"L53";
return true;
}
};