Valid Parentheses
2015-09-08 08:44
204 查看
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.
题意:
有效括号:给定一个字符串仅包含字符’(’, ’)’, ’{’, ’}’, ’[’和 ’]’,判断输入的字符串是否有效。
必须以正确的符号结束。
分析:
基础知识:
string::npos
npos是一个常数,用来表示不存在的位置,类型一般是std::container_type::size_type
许多容器都提供这个东西。取值由实现决定,一般是-1,这样做,就不会存在移植的问题了。
The brackets must close in the correct order, ”()” and ”()[]” are all valid but ”(]” and ”([)]” are not.
题意:
有效括号:给定一个字符串仅包含字符’(’, ’)’, ’{’, ’}’, ’[’和 ’]’,判断输入的字符串是否有效。
必须以正确的符号结束。
分析:
基础知识:
string::npos
npos是一个常数,用来表示不存在的位置,类型一般是std::container_type::size_type
许多容器都提供这个东西。取值由实现决定,一般是-1,这样做,就不会存在移植的问题了。
class Solution { public: bool isValid(string s) { string left="([{"; string right=")]}"; stack<char> stk; for(auto c:s) { if(left.find(c)!=string::npos) { stk.push(c); } else { if(stk.empty()||stk.top()!=left[right.find(c)]) return false; else stk.pop(); } } return stk.empty(); } };
相关文章推荐
- 六大设计原则--开闭原则
- 采用非常规方法(非gprecoverseg) 恢复greenplum数据库
- Android 垂直与水平滚动条:Scrollview与HorizontalScrollView
- Mind-manager之大话设计模式 22章桥接模式
- 微软Win10 Mobile/PC通用版《114查号》功能介绍以及免费下载
- HDU 5347 MZL's chemistry(水~)
- HDU 5339 Untitled(枚举)
- [Switf]下标语法subscript简单举例
- HDU 3746 Cyclic Nacklace(kmp)
- java线程死锁例子及解决方法
- C# Attribute(特性)之---数据契约 [DataContract]
- C# Attribute(特性)之---数据契约 [DataContract]
- iOS开发日记15-强制弹出系统键盘(禁用搜狗键盘)
- HDU 3652 B-number(数位DP)
- HDU 3584 Cube(三维BIT+容斥)
- HDU 3336 Count the string(kmp+dp)
- HDU 2614 Beat(dfs)
- HDU 2602 Bone Collector(01背包问题)
- HDU 2594 Simpsons' Hidden Talents(kmp)
- 谷歌返华,是“狼来了”还是狼来了?