leetcode-20-Valid Parentheses
2015-07-26 21:17
483 查看
Valid Parentheses
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.
括号匹配,使用栈求解。
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.
括号匹配,使用栈求解。
class Solution { public: bool isValid(string s) { stack<char>a; int n = s.size(); for (int i = 0;i < n;i++) { if (s[i] == '(' || s[i] == '{' || s[i] == '[') { a.push(s[i]); } else if (s[i] == ')' || s[i] == '}' || s[i] == ']') { if (a.empty()) return false; if (s[i] == ')' && a.top() == '(') { a.pop(); continue; } if (s[i] == '}' && a.top() == '{') { a.pop(); continue; } if (s[i] == ']' && a.top() == '[') { a.pop(); continue; } return false; } } if (a.empty()) return true; else return false; } };
相关文章推荐
- 形形色色的下拉菜单 (菜单中文变英文+多级菜单)
- Android 四大组件学习之BroadcastReceiver四
- Mahout学习四--贝叶斯分类
- hdu 1533 Going Home (最小费用最大流)
- VC++中如何将字符串转换成整型数字
- DP SRM 661 Div2 Hard: ColorfulLineGraphsDiv2
- mac上安装mysql
- Android清除本地数据缓存代码
- BZOJ3456 : 城市规划
- 黑马程序员----基础题----我的基础题
- 动态类型检查
- 我不是蛤蟆!
- Android ViewPager多页面滑动切换以及动画效果
- 代码检查、走查和评审
- A New Tetris Game - HDU 1760 博弈
- C# 抽象类和接口的区别
- 7.26 继承
- 0的个数(极速版)
- 为何马云总能五百块钱办成大事
- 7. PHP 变量