-
Notifications
You must be signed in to change notification settings - Fork 2
/
20Valid_ParenthesesLC.cpp
63 lines (42 loc) · 1021 Bytes
/
20Valid_ParenthesesLC.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
#include<iostream>
#include<string>
#include<stack>
using namespace std ;
class Solution {
public:
bool isValid(string s) {
stack<char> bass;
int i=0;
if(s.size()%2!=0)
return false;
while(i<s.size())
{
if(s[i]=='{'||s[i]=='['||s[i]=='(')
bass.push(s[i]);
else if(s[i]==']'||s[i]=='}'||s[i]==')')
{
if(bass.empty())
return false;
else if (s[i]==']'&&bass.top()!='[')
return false;
else if (s[i]=='}'&&bass.top()!='{')
return false;
else if (s[i]==')'&&bass.top()!='(')
return false;
else
bass.pop();
}
++i;
}
if(bass.empty())
return true;
else
return false;
}
};
int main()
{
Solution s1;
string g("(");
cout<<s1.isValid(g);
}