Leetcode挑战:Valid Parentheses(有效的括号)
2019-08-16 11:45
239 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hx19960205/article/details/99675035
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
解题思路:用一个栈遍历字符串左括号进栈,右括号时出栈(由于题中只包含左右括号,所以不必判读字符是不是右括号),当遍历过程中遇到当前字符为右括号,栈空时直接返回false,或者栈顶字符与右括号不匹配时返回false,遍历结束看栈是否为空,不为空即代表有左括号未匹配。
相关文章推荐
- LeetCode 20. Valid Parentheses--验证括号是否有效
- [LeetCode]35. Valid Parentheses有效括号
- 【LeetCode】- Valid Parentheses(有效的括号)
- 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】
- LeetCode | 32. Longest Valid Parentheses——最长有效括号匹配长度
- leetCode 32.Longest Valid Parentheses (有效的最大括号) 解题思路和方法
- [LeetCode] Longest Valid Parentheses 最长有效括号
- Leetcode刷题记——20. Valid Parentheses (有效的括号即括号匹配)
- [LeetCode] Valid Parentheses 有效的括号
- LeetCode Valid Parentheses 有效括号
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
- [Leetcode] longest valid parentheses 最长的有效括号
- leetcode 32. Longest Valid Parentheses 最长有效括号长度
- LeetCode 32 Longest Valid Parentheses(最长有效括号)(*)
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
- leetcode Valid Parentheses 有效括号
- LeetCode 20 Valid Parentheses(有效的括号)
- LeetCode 32. Longest Valid Parentheses(最长有效括号)
- LeetCode( Valid Parentheses)有效的括号
- [Leetcode] valid parentheses 有效括号对