[string]Valid Parentheses
2015-12-04 12:59
351 查看
Given a string containing just the characters
The brackets must close in the correct order,
'(',
')',
'{',
'}',
'['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> stk; int size = s.size(); for(int i=0;i<size;i++){ if(s[i]=='('||s[i]=='['||s[i]=='{'){ stk.push(s[i]); }else{ if(stk.empty()){ return false; } char topCh = stk.top(); if((s[i]==']' && topCh=='[' )|| (s[i]==')' && topCh=='(' )|| (s[i]=='}' && topCh=='{' ) ){ stk.pop(); }else{ return false; } } } return stk.empty()? true:false; } };
相关文章推荐
- 富士康投巨资要转型:布局二手智能手机市场
- Linux_DHCP&DHCP Relay
- android下拉菜单 spinner 学习
- 屏幕适配
- java 试题java编译问题
- 波导新款手机亮相工信部:三大实体键是汉字!
- 鲍尔默炮轰纳德拉提出的通用Windows平台战略
- A1486. 树(王康宁)
- poj 1088 滑雪 (记忆化搜索)
- spring3 mvc框架乱码问题
- Java工程师如何在Docker上进行开发
- OC-5.NSArray
- Salesforce CEO:鲍尔默和其他CEO关系比较糟糕
- +_________________________________________格式___________________________________1170
- iOS网络监测方法
- Edit――Sublime快捷键
- 允许其他应用程序启动你的Activity
- jsp + tomcat 连接Oracle数据实例
- activity的生命周期
- perl 数组元素为空和数组元素为undef 是两码事