-
Notifications
You must be signed in to change notification settings - Fork 1
/
20. Valid Parentheses
66 lines (62 loc) · 1.29 KB
/
20. Valid Parentheses
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
59
60
61
62
63
64
65
66
/*简单的括号匹配,用栈来处理就好了*/
#define max_size 1000001
class Solution {
public:
typedef struct SqStack{
int top;
char data[max_size];
}Stack;
void Init_Stack(Stack &s)
{
s.top = -1;
}
int PopStack(Stack &s,char &c)
{
if(s.top==-1)
return 0;
c = s.data[s.top--];
return 1;
}
int PushStack(Stack &s,char c)
{
if(s.top==max_size)
return 0;
s.data[++s.top] = c;
return 1;
}
bool isValid(string s) {
Stack st;
Init_Stack(st);
int i=0;
char c;
while(s[i]!='\0')
{
if(s[i]=='('|| s[i] =='{' ||s[i]=='[')
PushStack(st,s[i]);
else{
if(s[i]==')')
{
PopStack(st,c);
if(c!='(')
return false;
}
if(s[i]==']')
{
PopStack(st,c);
if(c!='[')
return false;
}
if(s[i]=='}')
{
PopStack(st,c);
if(c!='{')
return false;
}
}
i++;
}
if(st.top==-1)
return true;
return false;
}
};