LeetCode 20: Valid Parentheses
2015-06-02 22:05
447 查看
Given a string containing just the characters
The brackets must close in the correct order,
题目要求判断括号是否匹配,可以通过栈来实现。
代码如下:
'(',
')',
'{',
'}',
'['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) { stack<char> opstack; char c ; for (int i=0; i<s.size(); i++) { c = s[i]; if ((c=='(') || (c=='[') || (c=='{')) { opstack.push(c); } else if ((c==')') || (c==']') || (c=='}')) { if (opstack.empty()) { return false; } char tmp = opstack.top(); if ((c==')' && tmp=='(') || (tmp=='[' &&c==']' ) ||(tmp=='{' && c=='}')) { opstack.pop(); }else{ return false; } } } return opstack.empty(); } };
相关文章推荐
- django web 中添加超链接
- Binary Tree Level Order Traversal II
- 最后期限2
- ORA-01940:无法删除当前已链接的用户
- 解决ORA-28000: the account is locked
- 终于解决“Git Windows客户端保存用户名与密码”的问题
- iOS开发系列--UITableView全面解析
- 文本处理
- LeetCode 19: Remove Nth Node From End of List
- [Android Studio 权威教程]Windows下安装Android Studio
- Android播放视频
- PICT的安装和使用
- [Objective-C 面试简要笔记]
- java.lang.ClassNotFoundException: org.apache.http.util.Args
- nginx强制使用https访问(http跳转到https)
- 免费电子书:C#代码整洁之道
- 在VMware上安装Ubuntu14.04 , Docker
- 解决Linux(ubuntu),windows双系统重装后恢复开机选单
- 【Struts0002】Difference between # , $ and % signs in Struts2
- Solution to Triangle by Codility