Leetcode - 20. Valid Parentheses
2016-05-22 20:19
337 查看
题目介绍
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.
解法 using stack
bool isValid(string s) { stack<char> paren; for (char& c : s) { switch (c) { case '(': case '{': case '[': paren.push(c); break; case ')': if (paren.empty() || paren.top()!='(') return false; else paren.pop(); break; case '}': if (paren.empty() || paren.top()!='{') return false; else paren.pop(); break; case ']': if (paren.empty() || paren.top()!='[') return false; else paren.pop(); break; default: ; // pass } } return paren.empty() ; }
相关文章推荐
- 银行系统资料的整理
- LeetCode OJ 11. Container With Most Water
- Android 学习资源[转]
- 5月21日 JavaScrip应用t练习
- 6.css文本样式
- 第12周项目1-实现复数类中的运算重载(2)
- 游标
- 2021 中庸之道
- 自组织神经网络:Kohonen网络训练算法
- content = "IE=edge,chrome=1" 详解
- 建立开发板与PC机之间的nfs服务器
- 软件工程进度条-第十二周
- 强悍的 Linux —— 强悍的 vim
- C++走向远洋——52(十三周阅读程序)
- [深入理解Android卷二 全文-第五章]深入理解PowerManagerService
- C++中智能指针的设计和使用
- 回顾一些小函数
- 大学生扎推互联网创业后的生死录
- MIT6.828 Lab2: Challenge
- 响应式网页中,如何只用CSS实现div的高和宽保持固定比例