LeetCode | Valid Parentheses(括号匹配)
2014-07-08 11:06
429 查看
Given a string containing just the characters
determine if the input string is valid.
The brackets must close in the correct order,
all valid but
not.
题意很简单,就是判断是否匹配。通过栈来实现,如果左括号等,就入栈,如果右括号类,就出栈并判断是否匹配。当全部匹配完了,还要判断栈是否为空,不为空,也代表匹配不成功!
完整代码如下:
'(',
')',
'{',
'}',
'['and
']',
determine if the input string is valid.
The brackets must close in the correct order,
"()"and
"()[]{}"are
all valid but
"(]"and
"([)]"are
not.
题意很简单,就是判断是否匹配。通过栈来实现,如果左括号等,就入栈,如果右括号类,就出栈并判断是否匹配。当全部匹配完了,还要判断栈是否为空,不为空,也代表匹配不成功!
void ValidParentheses(char *str) { if(str == NULL){ printf("empty\n"); return ; } char ch; SqStack S; int len = strlen(str); InitStack(&S); if(len % 2){ printf("odd\n"); return; } for(int i = 0;i < len;++i){ if(str[i] == '(' || str[i] == '[' || str[i] == '{'){ Push(&S,str[i]); }else if(str[i] == ')' || str[i] == ']' || str[i] == '}'){ if(IsEmpty(S)){ //在POP函数中已经设置了错误判断,如果为空,就报错并退出循环。 printf("invalid!\n"); //但是直接退出程序不好,所有就提前判断是否为空,为空就报错,退出函数即可。 return; } ch = Pop(&S); if(!Match(ch,str[i])){ printf("invalid\n"); return ; } }else{ //处理输入其他字符的情况 printf("the char doesn't correct!\n"); return ; } } if(!IsEmpty(S)){ printf("doesn't match!\n"); }else printf("match!\n"); }
完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACKSIZEINIT 50
#define INCREMENT 10
typedef struct{
char *base;
char *top;
int stacksize;
}SqStack;
void InitStack(SqStack *S)
{
S->base = (char *)malloc(STACKSIZEINIT * sizeof(char));
if(S->base == NULL){
printf("malloc error\n");
exit(1);
}
S->top = S->base;
S->stacksize = STACKSIZEINIT;
}
void Push(SqStack *S,char ch)
{
if(S->top - S->base >= S->stacksize){
S->base = (char *)realloc(S->base,(S->stacksize + INCREMENT)*sizeof(char));
if(S->base == NULL){
printf("malloc error\n");
exit(1);
}
S->top = S->base + S->stacksize;
S->stacksize += INCREMENT;
}
*(S->top++) = ch;
}
int IsEmpty(SqStack S)
{
return S.base == S.top;
}
char Pop(SqStack *S)
{
char ch;
if(IsEmpty(*S)){
printf("stack empty\n");
exit(1);
}
ch = *(--S->top);
return ch;
}
int Match(char a,char b)
{
switch(a){
case '(':
return b == ')';
case '[':
return b == ']';
case '{':
return b == '}';
default :
return 0;
}
}
void ValidParentheses(char *str) { if(str == NULL){ printf("empty\n"); return ; } char ch; SqStack S; int len = strlen(str); InitStack(&S); if(len % 2){ printf("odd\n"); return; } for(int i = 0;i < len;++i){ if(str[i] == '(' || str[i] == '[' || str[i] == '{'){ Push(&S,str[i]); }else if(str[i] == ')' || str[i] == ']' || str[i] == '}'){ if(IsEmpty(S)){ //在POP函数中已经设置了错误判断,如果为空,就报错并退出循环。 printf("invalid!\n"); //但是直接退出程序不好,所有就提前判断是否为空,为空就报错,退出函数即可。 return; } ch = Pop(&S); if(!Match(ch,str[i])){ printf("invalid\n"); return ; } }else{ //处理输入其他字符的情况 printf("the char doesn't correct!\n"); return ; } } if(!IsEmpty(S)){ printf("doesn't match!\n"); }else printf("match!\n"); }
int main()
{
char buf[20];
while(scanf("%s",buf)!=EOF){
ValidParentheses(buf);
}
return 0;
}
相关文章推荐
- LeetCode 20 Valid Parentheses(用栈判断括号匹配)
- leetcode:Valid Parentheses(有效括号匹配)
- 【leetcode】括号符匹配问题(Parentheses):Valid Parentheses|Generate Parentheses|LongestValid Parentheses
- LeetCode 20 Valid Parentheses(括号匹配)
- LeetCode | Longest Valid Parentheses(最长有效的括号匹配)
- leetcode 20 Valid Parentheses 括号匹配
- LeetCode 20 Valid Parentheses 括号匹配问题
- leetcode 20 Valid Parentheses 括号匹配
- leetcode题解:Valid Parentheses(栈的应用-括号匹配)
- LeetCode Longest Valid Parentheses 括号匹配
- leetcode 20 Valid Parentheses 括号匹配
- Longest Valid Parentheses(最长的括号匹配)【面试算法leetcode】
- LeetCode 32 Longest Valid Parentheses 最大合法括号匹配长度计算 动态规划算法有待学习
- LeetCode | 32. Longest Valid Parentheses——最长有效括号匹配长度
- LeetCode20——Valid Parentheses(括号匹配问题,使用栈的知识)
- Leetcode 20 Valid Parentheses 判断括号匹配
- LeetCodet题解--20. Valid Parentheses(括号匹配问题)
- LeetCode 20 Valid Parentheses (括号匹配问题)
- LeetCode 32 Longest Valid Parentheses (栈,括号匹配)
- LeetCode-Longest Valid Parentheses-最长匹配括号-栈的应用