Valid Parentheses
来源:互联网 发布:ubuntu修改文件所有者 编辑:程序博客网 时间:2024/05/22 06:22
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 len = s.length(); if (len & 1) { return false; } if (len < 1) { return true; } stack<char> buf; buf.push(s[0]); for (int i = 1; i < len; i++) { char top = ' '; if (!buf.empty()) { top = buf.top(); } if (s[i] == ')') { if (top == '(') { buf.pop(); } else { return false; } } else if (s[i] == ']') { if (top == '[') { buf.pop(); } else { return false; } } else if (s[i] == '}') { if (top == '{') { buf.pop(); } else { return false; } } else { buf.push(s[i]); } } if (!buf.empty()) { return false; } return true; }};
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
- libevent学习的资料
- code for qint function
- Qt5 error LNK2019 无法解析的外部符号 解决办法
- 欢迎使用CSDN-markdown编辑器
- 实数,有理数,无理数,自然数,整数的概念分别是什么?
- Valid Parentheses
- IOS客户端公共WIFI解决方案
- 使用struts、hibernate、spring进行框架整合流程详细说明(完整版)附:开发规范
- iOS AutoLayout 设置多个相同间隔的视图
- Spring 框架的设计理念与设计模式分析
- 深入理解Objective-C的Block
- java学习笔记1--win下java环境变量的配置
- 飞速换卡应用sha256加密
- 新风格风格个