【LeetCode】C# 20、Valid Parentheses
2017-10-09 21:33
483 查看
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.
给定一个字符串,只包含括号类的符号,判断是否每个括号都为closed,是否都符合语法。
解题关键在利用堆栈后进先出的特性。看是否每个左括号都有对应的右括号
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
给定一个字符串,只包含括号类的符号,判断是否每个括号都为closed,是否都符合语法。
解题关键在利用堆栈后进先出的特性。看是否每个左括号都有对应的右括号
public class Solution { public bool IsValid(string s) { Stack<Char> stack = new Stack<Char>(); for (int i = 0; i < s.Length; i++) { Console.WriteLine(s[i]); if (s.Length == 0) return false; if (s[i] == '(') { stack.Push(')'); } else if (s[i] == '{') { stack.Push('}'); } else if (s[i] == '[') { stack.Push(']'); } else if (stack.Count == 0) return false; else if (s[i] == stack.Peek()) { stack.Pop();Console.WriteLine("pop"); } else return false; } if (stack.Count == 0) return true; return false; } }
相关文章推荐
- Leetcode[20]-Valid Parentheses
- 【leetcode】20. Valid Parentheses
- LeetCode20. Valid Parentheses
- leetcode --20. Valid Parentheses
- LeetCode 20 Valid Parentheses (括号匹配问题)
- Leetcode 20 - Valid Parentheses
- leetcode 20. Valid Parentheses
- LeetCode 20: Valid Parentheses
- Leetcode题解14 20. Valid Parentheses
- [LeetCode-20] Valid Parentheses(用栈解决配对问题)
- Leetcode 题目 20. Valid Parentheses(个人错误分析)
- Leetcode 20. Valid Parentheses - javascript
- [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