Valid Parentheses
2024-08-25 05:25:18
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.
Subscribe to see which companies asked this question
class Solution {
public:
bool isValid(string s) {
stack<char> store;
for (auto c : s) {
if (c == '(' || c == '{' || c == '[')
store.push(c);
else {
if (store.empty()) return false;
if (c == ')' && store.top() == '('){
store.pop();
}
else if (c == '}' && store.top() == '{'){
store.pop();
}
else if (c == ']' && store.top() == '['){
store.pop();
}
else
return false;
}
}
return store.empty();
}
};
最新文章
- Java的概述以及语法
- BZOJ 1014 【JSOI2008】 火星人prefix
- 《高级Web应用程序设计》课程学习资料
- javascript 的字符串原生方法
- asp.net 中json字符串转换
- (转)在javascript中关于submit和button提交表单区别
- OpenSUSE 13.2安装Texlive2014+Texmaker+Lyx
- C# 通过url地址获取页面内容
- 后端api规范说明文档
- Shell 自动化部署免密登录
- Essential Phone刷机教程
- Dan版本的nnet2
- 用Docker启动php-5.6 fpm服务配合宿主机nginx运行php测试环境
- Celery 在Windows下启动worker时出现错误:ValueError: not enough values to unpack (expected 3, got 0)
- Delphi控件备份工具
- (第七周)评论alpha发布
- [Selenium]刷新页面 Refresh page
- java变量的命名使用规则
- Javascript基本代码
- 伪差IO分标准