forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_stack_n.cpp
45 lines (40 loc) · 1.02 KB
/
AC_stack_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_stack_n.cpp
* Create Date: 2014-12-05 14:19:58
* Descripton: use stack
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
bool isValid(string s) {
stack<char> stk;
int len = s.length();
for (int i = 0; i < len; i++) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
stk.push(s[i]);
} else {
if (stk.empty())
return false;
if (stk.top() == '(' && s[i] == ')')
stk.pop();
else if (stk.top() == '[' && s[i] == ']')
stk.pop();
else if (stk.top() == '{' && s[i] == '}')
stk.pop();
else
return false;
}
}
return stk.empty();
}
};
int main() {
Solution a;
string s;
while (cin >> s)
cout << a.isValid(s) << endl;
return 0;
}