首页 > 学院 > 开发设计 > 正文

LeetCode 20. Valid Parentheses

2019-11-08 01:24:00
字体:
来源:转载
供稿:网友

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

程序代码:

bool isValid(char* s) { char stack[10000]; char* p= s; int i= 0; while(*p!='/0') { if(*p=='('||*p=='['||*p=='{') { i++; stack[i]=*p; p++; } else if(*p==')'&&stack[i]=='(') { i--; p++; } else if(*p==']'&&stack[i]=='[') { i--; p++; } else if(*p=='}'&&stack[i]=='{') { i--; p++; } else return 0; } if(i>0) return 0; else return 1;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表