Leetcode 20 - Valid Parentheses
来源:互联网 发布:python 汉字转url编码 编辑:程序博客网 时间:2024/06/06 09:49
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.
1 - 考查栈的基本应用
2 - std::string::npos
static const size_t npos = -1;
Maximum value for size_t
npos is a static member constant value with the greatest possible value for an element of type size_t.
This value, when used as the value for a len (or sublen) parameter in string’s member functions, means “until the end of the string”.
As a return value, it is usually used to indicate no matches.
This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type.
class Solution {public: bool isValid(string s) { string left = "({["; string right = ")}]"; stack<char> stk; for(char c : s){ //若字符为左括号,则入栈 if(left.find(c)!=string::npos){ stk.push(c); //若字符为右括号时,栈空或者和左括号无法匹配,则返回false }else if(stk.empty()||left[right.find(c)]!=stk.top()){ return false; //否则左括号出栈 }else{ stk.pop(); } } if(stk.empty()) return true; else return false; }};
- LeetCode 20: Valid Parentheses
- LeetCode(20) Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode【20】:Valid Parentheses
- leetcode 20-Valid Parentheses
- [leetcode 20] Valid Parentheses
- [Leetcode] 20 - Valid Parentheses
- LeetCode | #20 Valid Parentheses
- leetcode.20-----------Valid Parentheses
- [leetcode] 20 Valid Parentheses
- leetcode-20 Valid Parentheses
- leetCode #20 Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode 20 Valid Parentheses
- LeetCode 20 - Valid Parentheses
- LeetCode 20: Valid Parentheses
- leetcode-20 Valid Parentheses
- leetcode 20 -- Valid Parentheses
- Leetcode 130. Surrounded Regions
- Leetcode 18 - 4Sum
- Epoll 新增 EPOLLEXCLUSIVE 选项解决了新建连接的’惊群‘问题
- nginx_lua 扩展让 nginx 拥有可编程能力
- Nginx 工作进程运行示意图
- Leetcode 20 - Valid Parentheses
- 安卓popupwindow弹出窗体的使用
- Leetcode 21 - Merge Two Sorted Lists
- 资深谷歌安卓工程师对安卓应用开发的建议
- 向 Windows 窗体添加菜单和菜单项
- 2276: [Poi2011]Temperature 单调队列
- RDD实战
- Leetcode 22 - Generate Parentheses
- 开放在线课堂平台推荐