-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode20.cpp
More file actions
41 lines (39 loc) · 838 Bytes
/
leetcode20.cpp
File metadata and controls
41 lines (39 loc) · 838 Bytes
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
#include <iostream>
#include <cstring>
#include <string>
#include <stack>
using namespace std;
class Solution {
public:
bool isValid(string s) {
stack<char> str;
for(int i=0;i<s.length();i++){
switch(s[i])
{
case '{':
case '[':
case '(':str.push(s[i]);break;
case '}':
case ']':
case ')':char temp=s[i];
if(str.empty())
return false;
else if((temp=='}'&&str.top()=='{')||(temp==']'&&str.top()=='[')||(temp==')'&&str.top()=='('))
str.pop();
else{
return false;
}
}
}
if(!str.empty()){
return false;
}
else
return true;
}
};
int main()
{
Solution s1;
cout<<s1.isValid("]")<<endl;
}