leetcode 20-Valid Parentheses
2014-12-26 15:03
429 查看
原题:
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.
即判断一串括号是否匹配。
思路:
这是一个可以用栈(stack)来解决的经典问题,具体可以按照如下几步来实现:
1. 设输入的一串括号为s,如果s为空,则返回false。如果s长度为奇数,则必然无法匹配,同样返回false。
2. 将s的第一个字符压入栈,开始遍历s, 并循环执行下面的操作。
3. 如果此时栈为空,则分两种情况:
—— 3.1当前字符属于左括号,则将其入栈。
—— 3.2当前字符属于右括号,则s必然无法匹配,直接返回false。
4. 如果此时栈不为空,又分为如下几种情况:
—— 4.1 如果s的当前字符与栈顶元素可以匹配成一对括号,则将栈顶元素出栈。
—— 4.2 如果不匹配,则分为两种情况:
———— 4.2.1 如果s当前字符属于左括号,则将其入栈。
———— 4.2.2 如果s当前字符属于右括号,则直接返回false。
5. 遍历完s,如果发现此时栈为空,说明s中的括号完全匹配,返回true,否则返回false。
根据上述思路,可以轻松写出下面的Java代码(使用了Java Util中自带的Stack类)
代码(Java):
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.
即判断一串括号是否匹配。
思路:
这是一个可以用栈(stack)来解决的经典问题,具体可以按照如下几步来实现:
1. 设输入的一串括号为s,如果s为空,则返回false。如果s长度为奇数,则必然无法匹配,同样返回false。
2. 将s的第一个字符压入栈,开始遍历s, 并循环执行下面的操作。
3. 如果此时栈为空,则分两种情况:
—— 3.1当前字符属于左括号,则将其入栈。
—— 3.2当前字符属于右括号,则s必然无法匹配,直接返回false。
4. 如果此时栈不为空,又分为如下几种情况:
—— 4.1 如果s的当前字符与栈顶元素可以匹配成一对括号,则将栈顶元素出栈。
—— 4.2 如果不匹配,则分为两种情况:
———— 4.2.1 如果s当前字符属于左括号,则将其入栈。
———— 4.2.2 如果s当前字符属于右括号,则直接返回false。
5. 遍历完s,如果发现此时栈为空,说明s中的括号完全匹配,返回true,否则返回false。
根据上述思路,可以轻松写出下面的Java代码(使用了Java Util中自带的Stack类)
代码(Java):
public class Solution { boolean isMatch(char top, char target) //辅助函数 判断两个括号是否匹配 { if(top=='(' && target==')') { return true; } else if(top=='[' && target==']') { return true; } else if(top=='{' && target=='}') { return true; } else { return false; } } boolean isLeft( char target) //辅助函数 判断括号是否为左括号 { if(target=='('||target=='['||target=='{') { return true; } else { return false; } } boolean isRight( char target) //辅助函数 判断括号是否为右括号 { if(target==')'||target==']'||target=='}') { return true; } else { return false; } } public boolean isValid(String s) { if(s.equals("")||s==null) { return false; } int length = s.length(); if(length%2==1) { return false; } Stack<Character> temp = new Stack<Character>(); int i = 0; while(i < length) { char target = s.charAt(i); if(temp.isEmpty()) //如果栈为空,并且为左括号,则压栈,否则必定无法匹配,返回false { if(isLeft(target)) { temp.push(target); } else { return false; } } else { char top = temp.peek(); if(isMatch(top,target)) //如果匹配,则栈顶出栈 { temp.pop(); } else if(isLeft(target)) //如果不匹配,当前括号为左括号则压栈 { temp.push(target); } else //如果不匹配,当前括号为右括号则返回false { return false; } } ++i; } if(temp.isEmpty()) { return true; } return false; } }
相关文章推荐
- LeetCode20:Valid Parentheses
- [leetcode 20] Valid Parentheses
- leetcode-20. Valid Parentheses
- [Leetcode] 20. Valid Parentheses(Stack)
- leetcode_20_Valid Parentheses (easy)
- LeetCode 20 -- Valid Parentheses
- [Leetcode] 20. Valid Parentheses
- Leetcode: 20. Valid Parentheses
- leetcode 20. Valid Parentheses
- 20. Valid Parentheses-leetcode-java
- [leetcode]20. Valid Parentheses(java)
- leetcode之top100_01_twoSsum/top100_20_ValidParentheses
- [LeetCode-Algorithms-20] "Valid Parentheses" (2017.11.1-WEEK9)
- [leetcode Q20] Valid Parentheses
- LeetCodet题解--20. Valid Parentheses(括号匹配问题)
- leetcode 20. Valid Parentheses
- 【leetcode】20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- leetcode 20. Valid Parentheses
- leetcode 20. Valid Parentheses