LeetCode题解——Valid Parentheses
2015-07-14 19:53
316 查看
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.
重在对栈的使用
class Solution { public: bool isValid(string s) { if(s.size()%2!=0 ) return false; stack<char> st; for(int i=0;i<s.size();i++) { if(st.empty()) st.push(s[i]); else { char p = st.top(); if((p=='(' && s[i]==')')|| (p=='['&&s[i]==']') || (p=='{' && s[i]=='}')) st.pop(); else st.push(s[i]); } } return st.empty(); } };
相关文章推荐
- UVALive 4108 SKYLINE(线段树区间更新)
- 父对象和子对象---(OC面向对象--多态)
- 模态类型的传值问题
- [leetcode刷题笔记]Implement Trie (Prefix Tree)
- MarkDown的使用、JAVA环境变量的配置、JAVA基础
- 接触式IC卡
- 汉诺塔问题
- Python Idle 无法显示行号(上)
- web项目部署到linux服务器出现的问题
- 使用蒲公英来做iOS测试应用的分发
- UVa 10285 - Longest Run on a Snowboard
- 无限分类树操作
- espcms内容页相册调用代码
- c++对象模型-the semantics of constr ad97 uctors
- 【SeekBar】Android 自定义漂亮的SeekBar样式
- debian 8下修改默认编辑器
- C8_指针练习
- LeetCode题解——Generate Parentheses
- 大楼扔鸡蛋问题(动态规划)
- 【Spark】SparkContext源码解读