leetcode 20. Valid Parentheses
2016-05-26 09:18
381 查看
题目:
Given a string containing just the characters
The brackets must close in the correct order,
括号匹配问题
利用堆栈匹配
bool isValid(char* s) {
//构造一个堆栈,存放s
int len=strlen(s);
if(len==0)
return true;
char * stack=(char *)malloc(sizeof(char)*len);
int top=0;
int i;
for(i=0;i<len;i++)
{
if(top==0)
{
stack[top]=s[i];
top++;
}
else//top>0时
{
if( (stack[top-1] == '(' && s[i]==')') || (stack[top-1]=='[' && s[i]==']') || (stack[top-1]=='{' && s[i]=='}') )
{
top--;
}
else
{
stack[top]=s[i];
top++;
}
}
}
if(top==0)
return true;
else
return false;
}
题目:
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) {
//构造一个堆栈,存放s
int len=strlen(s);
if(len==0)
return true;
char * stack=(char *)malloc(sizeof(char)*len);
int top=0;
int i;
for(i=0;i<len;i++)
{
if(top==0)
{
stack[top]=s[i];
top++;
}
else//top>0时
{
if( (stack[top-1] == '(' && s[i]==')') || (stack[top-1]=='[' && s[i]==']') || (stack[top-1]=='{' && s[i]=='}') )
{
top--;
}
else
{
stack[top]=s[i];
top++;
}
}
}
if(top==0)
return true;
else
return false;
}
相关文章推荐
- Gradle 1.12用户指南翻译——第四十七章. Build Init 插件
- Gradle 1.12用户指南翻译——第四十七章. Build Init 插件
- 【C】printf的返回值
- win10系统调用架构分析
- 《上古天真论》第十讲文字版
- HDU 5696 ——区间的价值——————【线段树、快排思想】
- 第13周实践项目4立体类族共有的抽象类
- classloader介绍以及自定义classloader的初步认知
- 移动开发之设计稿转换页面单位尺寸
- 第十二周项目3—数组类运算的实现
- android 加载图片本地有缓存,如何实时更新服务器上修改的图片
- C++ 容器 vector
- 常用mysql语句
- css-去除默认样式
- Several exercises
- [python]新手写爬虫v2.5(使用代理的异步爬虫)
- 程序员等级能力模型
- String直接赋字符串和new String的区别
- http in iOS
- 巧用location.hash保存页面状态