[LeetCode]Valid Parentheses
2015-11-12 10:09
369 查看
题目描述:(链接)
Given a string containing just the characters
The brackets must close in the correct order,
解题思路:
用一个stack实现
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实现
class Solution { public: bool isValid(string s) { for (int i = 0; i < s.size(); ++i) { switch (s[i]) { case '(': case '{': case '[': { cache.push(s[i]); break; } case ')': { if (!isSuitable('(')) { return false; } break; } case '}': { if (!isSuitable('{')) { return false; } break; } case ']': { if (!isSuitable('[')) { return false; } break; } } } if (!cache.empty()) return false; return true; } private: stack<int> cache; bool isSuitable(int character) { if (cache.empty()) { return false; } int ch = cache.top(); if (ch != character) { return false; } cache.pop(); return true; } };
相关文章推荐
- Comb sort: Sorting by Exchanging
- Unofficial Windows Binaries for Python Extension Packages
- Android的系统服务一览
- Windows下配置Redis环境
- 论创新
- Easysize 的使用
- SDN的前途几何?
- C++面试出现频率最高的30道题目(一)
- 穿梭车特点介绍
- Android简洁漂亮的引导页
- libva编译与测试
- 用android studio将assets打包到jar文件的方法
- seo 和 网页跳转代码 的关系
- Java学习笔记(十三)I/O流
- bzoj1022.小约翰的游戏John(博弈论 NIM游戏)
- css实现鼠标经过导航颜色变换效果
- WaitAny, WaitAll 和 SignalAndWait
- offsetwidth/clientwidth的区别
- 深入浅析Python字符编码
- cocos2d-x Programmers Guide v3.3 译本和阅读笔记(第5章:创建和转换场景)