Valid Parentheses
来源:互联网 发布:义乌管家婆软件jhgjp 编辑:程序博客网 时间:2024/06/06 04:33
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,这样做,就不会存在移植的问题了。
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(); }};
0 0
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Javascript的this用法
- ubuntu shell不兼容
- mod_jk负载均衡 Request Entity Too Large
- 资源记录
- 用能看的懂的方法模拟约瑟夫环
- Valid Parentheses
- iOS-添加pch文件
- iOS 集成银联支付
- 设计模式:“六大设计原则”
- Word2010怎么绘制斜线表头
- Spring学习笔记-Spring容器
- 安卓开发黄金搭档:android-studio+Genymotion模拟器
- java控制台中打印万年历
- 简化SQL式计算之区间合并