HackRank Stacks: Balanced Brackets;/Leetcode valid-parentheses; 括号匹配
2016-09-29 23:05
465 查看
原题链接:https://www.hackerrank.com/contests/master/challenges/ctci-balanced-brackets?h_r=internal-search
Given a string containing just the characters
determine if the input string is valid.
The brackets must close in the correct order,
all valid but
not.
Sample Input
Sample Output
更多例子:
"()" yes;
")(" no;
"(abcd(e)" no;
"(a)(b)" yes。
([)] No(特别要注意这个测试用例)
Explanation
The string
both criteria for being a balanced string, so we print
a new line.
The string
not balanced, because the brackets enclosed by the matched pairs
not balanced. Thus, we print
a new line.
The string
both criteria for being a balanced string, so we print
a new line.
题目出自HackRank
思路:
1 使用Stack<char>处理;
2 程序开始和结束时,stack都应该是空的;
3 程序中途应该注意匹配
C#代码:
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.
Sample Input
3 {[()]} {[(])} {{[[(())]]}}
Sample Output
YES NO YES
更多例子:
"()" yes;
")(" no;
"(abcd(e)" no;
"(a)(b)" yes。
([)] No(特别要注意这个测试用例)
Explanation
The string
{[()]}meets
both criteria for being a balanced string, so we print
YESon
a new line.
The string
{[(])}is
not balanced, because the brackets enclosed by the matched pairs
[(]and
(])are
not balanced. Thus, we print
NOon
a new line.
The string
{{[[(())]]}}meets
both criteria for being a balanced string, so we print
YESon
a new line.
题目出自HackRank
思路:
1 使用Stack<char>处理;
2 程序开始和结束时,stack都应该是空的;
3 程序中途应该注意匹配
C#代码:
/// <summary> /// 输入小括号、中括号、大括号,判断是否匹配; /// 举例: /// {[()]} YES ///{[(])} NO ///{{[[(())]]}} YES ///([)] No /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsBalancedBrackets(string str) { if (string.IsNullOrEmpty(str)) return false; Stack<char> stack = new Stack<char>(); char[] charArr = str.ToCharArray(); for (int i = 0; i < charArr.Length; i++) { if (charArr[i].Equals('(')) stack.Push(charArr[i]); else if (charArr[i].Equals('[')) stack.Push(charArr[i]); else if (charArr[i].Equals('{')) stack.Push(charArr[i]); else if (charArr[i].Equals(')')) { if (stack.Count == 0 || stack.Pop() != '(') return false; } else if (charArr[i].Equals(']')) { if (stack.Count == 0 || stack.Pop() != '[') return false; } else if(charArr[i].Equals('}')) { if (stack.Count == 0 || stack.Pop() != '{') return false; } } if (stack.Count == 0 && stack.Count == 0 && stack.Count == 0) return true; else return false; }
相关文章推荐
- 【leetcode】括号符匹配问题(Parentheses):Valid Parentheses|Generate Parentheses|LongestValid Parentheses
- LeetCode | Valid Parentheses(括号匹配)
- LeetCode Longest Valid Parentheses 括号匹配
- LeetCode--valid-parentheses(有效括号)
- LeetCode - 32 - Longest Valid Parentheses(括号匹配)
- LeetCodet题解--22. Generate Parentheses(生成n对匹配的括号)
- Leetcode刷题记——20. Valid Parentheses (有效的括号即括号匹配)
- 【LeetCode】最长括号匹配Longest Valid Parentheses
- LeetCode 20 Valid Parentheses(用栈判断括号匹配)
- LeetCode 20 Valid Parentheses(括号匹配)
- LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)
- LeetCode | 32. Longest Valid Parentheses——最长有效括号匹配长度
- LeetCode Valid Parentheses(判断括号是否匹配)
- LeetCodet题解--20. Valid Parentheses(括号匹配问题)
- leetcode 20 Valid Parentheses 括号匹配
- LeetCode20——Valid Parentheses(括号匹配问题,使用栈的知识)
- LeetCode | Longest Valid Parentheses(最长有效的括号匹配)
- LeetCode 20 Valid Parentheses 括号匹配问题
- leetcode 20 Valid Parentheses 括号匹配
- leetcode:Valid Parentheses(有效括号匹配)