Valid Parentheses
来源:互联网 发布:vscode lua 编辑:程序博客网 时间:2024/05/17 18:23
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; for(char& n:s) { switch(n) { case'(': case'{': case'[': a.push(n); break; case')': if(a.empty() || a.top()!='(') return false; else a.pop(); break; case'}': if(a.empty() || a.top()!='{') return false; else a.pop(); break; case']': if(a.empty() || a.top()!='[') return false; else a.pop(); break; default:; } } return a.empty()?true:false; }};
0 0
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- IQ使命 Rapa Nui 复活岛(智力大逃亡)攻略
- IQ使命 London 伦敦(一笔画)攻略
- iterm2+oh my zsh
- CSS规范
- Python进阶(三十七)-Windows7使用nginx+apache部署django项目
- Valid Parentheses
- Android Studio2.3NDK的简单配置及快速开发
- 公共子序列
- 整数的拆分
- 005-023-Merge k sorted lists 归并K个有序的链表
- 2017.4.15模拟赛总结
- 如何安装Composer
- 多线程操作例题
- swustoj(字符串查找(0448))