20.Valid Parentheses 栈的应用:括号匹配
2017-09-23 21:06
501 查看
十分简单的括号匹配,用栈来操作就可以了
#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;
}
};
#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;
}
};
相关文章推荐
- LeetCode 20 Valid Parentheses 括号匹配问题
- LeetCode 20 Valid Parentheses (括号匹配问题)
- Leetcode刷题记——20. Valid Parentheses (有效的括号即括号匹配)
- 20 Valid Parentheses(匹配括号)
- leetcode 20 Valid Parentheses 括号匹配
- leetcode 20 Valid Parentheses 括号匹配
- Leetcode 20 Valid Parentheses 判断括号匹配
- LeetCode20——Valid Parentheses(括号匹配问题,使用栈的知识)
- LeetCode-Longest Valid Parentheses-最长匹配括号-栈的应用
- LeetCode 20 Valid Parentheses(用栈判断括号匹配)
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
- leetcode 20 Valid Parentheses 括号匹配
- LeetCode 20 Valid Parentheses(括号匹配)
- 20. Valid Parentheses(括号匹配,用桟)
- LeetCodet题解--20. Valid Parentheses(括号匹配问题)
- leetcode题解:Valid Parentheses(栈的应用-括号匹配)
- Lettcode_20_Valid Parentheses——括号匹配算法
- 20. Valid Parentheses (括号匹配)
- LeetCode-20 Valid Parentheses(判断括号是否规范)
- Longest Valid Parentheses:最长括号子段匹配