Valid Parentheses --括号匹配
来源:互联网 发布:阿里域名价格 编辑:程序博客网 时间:2024/06/05 04:16
问题:链接
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> st; for(int i = 0; i < s.length(); ++i) {if(st.empty())st.push(s[i]); else if((st.top() == '[' && s[i] == ']') || (st.top() == '{' && s[i] == '}') || (st.top() == '(' && s[i] == ')')) st.pop(); else st.push(s[i]); } if(st.empty()) return true; return false; }};
0 0
- Valid Parentheses --括号匹配
- Valid Parentheses:括号匹配
- 20.Valid Parentheses (括号匹配)
- 多种括号的匹配 Valid Parentheses
- 【栈操作&括号匹配】Valid Parentheses
- LeetCode | Valid Parentheses(括号匹配)
- LeetCode 20 Valid Parentheses 括号匹配问题
- leetcode 20 Valid Parentheses 括号匹配
- Valid Parentheses 合法的括号匹配
- LeetCode Longest Valid Parentheses 括号匹配
- LeetCode Valid Parentheses(判断括号是否匹配)
- 20. Valid Parentheses (括号匹配)
- (leetcode)Valid Parentheses --- 括号匹配问题
- 【LeetCode】 括号匹配1 Valid Parentheses
- 【LeetCode】最长括号匹配Longest Valid Parentheses
- 【leetcode】括号符匹配问题(Parentheses):Valid Parentheses|Generate Parentheses|LongestValid Parentheses
- 寻找最长的括号匹配 Longest Valid Parentheses
- [LeetCode]—Longest Valid Parentheses 最长括号匹配
- Oracle中Hint深入理解(原创)
- CoreData第三方数据---MagicalRecord使用说明
- Android官方文档---优化下载让网络访问更高效(四)
- crm2011js子网格导航栏字段事件操作
- os x下如何挂载iso镜像
- Valid Parentheses --括号匹配
- ORACLE的HINT详解
- 程序员长期保持身心健康的几点建议
- Linux VM环境配置
- PHP调试工具FirePHP使用方法
- OpenCV学习笔记(3)——图像形态学操作(Mathematical Morphology)
- DPL,RPL,CPL 之间的联系和区别
- 迭代器的使用和注意事项
- ORACLE 中dbms_stats的使用