forked from mrchuanxu/RegularNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidParenthess.cc
40 lines (39 loc) · 1.07 KB
/
ValidParenthess.cc
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
#include <iostream>
#include <map>
#include <stack>
using namespace std;
class Solution {
public:
bool isValid(string s) {
// 会用栈去处理
if(s == "") return true;
// 先构一个map
// 然后用map比较
map<char,char> map_str = {{'(',')'},{'{','}'},{'[',']'}};
stack<char> sta_str;
sta_str.push(s[0]);
int i = 1;
bool flag = false;
map<char,char>::iterator map_iter;
while(i<s.size()){
if(sta_str.size()>0){
map_iter = map_str.find(sta_str.top());
if(map_iter!=map_str.end()){
if(map_iter->second==s[i]){
flag = true;
sta_str.pop();
}else{
flag = false;
sta_str.push(s[i]);
}
}else return flag;
}else {
flag = false;
sta_str.push(s[i]);
}
i++;
}
if(sta_str.size()>0) flag = false;
return flag;
}
};