Valid Parentheses
来源:互联网 发布:什么是科学精神知乎 编辑:程序博客网 时间:2024/06/05 14:09
题目:
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.
括号匹配,采用stack。
代码:
#include <iostream>#include <stack>#include <string>using namespace std;class Solution {public: bool isValid(string s) {stack<char> mystack;for(int i=0;i<s.size();++i){switch(s[i]){case '(':mystack.push(s[i]);break;case '[':mystack.push(s[i]);break;case '{':mystack.push(s[i]);break;case ')':if(!mystack.empty()&&mystack.top()=='('){mystack.pop();}else{return false;}break;case ']':if(!mystack.empty()&&mystack.top()=='['){mystack.pop();}else{return false;}break;case '}':if(!mystack.empty()&&mystack.top()=='{'){mystack.pop();}else{return false;}default:break;}} if(mystack.empty()) return true;else return false; }};int main(){string str;cin>>str;Solution s;bool ret=s.isValid(str);if(ret)cout<<"YES!"<<endl;elsecout<<"NO!"<<endl;system("pause");return 0;}
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
- 工作没干劲,想去培训,读完“Himi浅谈游戏开发de自学历程”有感!终于明白坚持才是王道!
- asterisk+dahdi+chan_ss7搭建E1呼叫小型测试环境(二)
- mysql 日期时间UNIX_TIMESTAMP、FROM_UNIXTIME 和DATE_FORMAT 基本使用方法
- 验证码安全问题汇总
- hadoop实例---多表关联
- Valid Parentheses
- 测量应用软件和系统软件瓶颈的工具集合
- C#中为什么要使用委托的?
- codeforces #549 Looksery Cup 部分题解
- 分治法求最大子数组备忘
- 解决FlexPaper分页分段加载问题,大文件预览
- 中国象棋实现 --- 记录日志
- 第六章
- Ibatis left join关联查询