LeetCode #20 Valid Parentheses
来源:互联网 发布:js点击刷新div内容 编辑:程序博客网 时间:2024/06/07 09:44
Description
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.
Analysis
题目难度为:Easy
括号匹配考查的是栈的出入栈问题——如果是左括号则入栈,如果是右括号,栈顶是相匹配的左括号则左括号出栈。当输入右括号,栈为空或者栈顶括号不匹配,则匹配出错。
Code(c++)
class Solution {public: bool isValid(string s) { stack<char> st; for (int i = 0; i < s.size(); ++i) { if (s[i] == '(' || s[i] == '[' || s[i] == '{') { st.push(s[i]); } else if (s[i] == ')') { if (!st.empty() && st.top() == '(') st.pop(); else return false; } else if (s[i] == ']') { if (!st.empty() && st.top() == '[') st.pop(); else return false; } else if (s[i] == '}') { if (!st.empty() && st.top() == '{') st.pop(); else return false; } } if (st.empty()) return true; return false; }};
阅读全文
0 0
- 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
- 树的实现
- <NOIP> 25 . P1217 [USACO1.5]回文质数 Prime Palindromes
- dlx模板
- 浅谈js之闭包
- java设计模式总结(1)
- LeetCode #20 Valid Parentheses
- django自定义模版函数
- java基本数据类型
- Java值传递以及引用的传递、数组的传递!!
- java中的protected
- cudaMallocHost函数详解
- 如何使用Linux
- vue.js开发环境搭建以及脚手架工具安装
- 1.简单工厂模式