Valid Parentheses
来源:互联网 发布:网络被攻击 编辑:程序博客网 时间:2024/05/22 13:02
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) { int nLen=s.length(); int i=0; stack<char> cs; while(i<nLen) { switch(s[i]) { case '[': case '{': case '(': cs.push(s[i]); break; case ']': { if(!cs.empty() && cs.top()=='[') { cs.pop(); break; } else { return false; } } case '}': { if(!cs.empty() && cs.top()=='{') { cs.pop(); break; } else { return false; } } case ')': { if(!cs.empty() && cs.top()=='(') { cs.pop(); break; } else { return false; } } default: break; } ++i; } if(cs.empty()) { return true; } else { return 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
- Remove Nth Node From End of List
- 黑马程序员——java基础学习笔记——第五天
- 黑马程序员——java基础——异常
- 基于 spring4 java config(配置)的servlet3 文件上传于下载
- 欢迎使用CSDN-markdown编辑器
- Valid Parentheses
- Rotate Array
- JStorm之Topology调度
- 一个球从100米高的自由落下,每次落地后反跳回原高度的一半,再落下,再反弹。求第 10次落地时,共经过多少米,第10次反弹多高。
- Path Sum
- 二叉排序树的一些简单操作
- 欢迎使用CSDN-markdown编辑器
- Minimum Depth of Binary Tree
- 策略模式