1. Title
Valid parentheses (with reasonable brackets)
2. Address of the topic
https://leetcode.com/problems/valid-parentheses/
3. Topic content
English: Given A string containing just the characters,,,, and ‘(‘
‘)‘
‘{‘
‘}‘
‘[‘
‘]‘
, determine if the input Strin G is valid. The brackets must close in the correct order, and is all valid but and is not "()"
"()[]{}"
"(]"
"([)]"
.
Chinese: Given a string containing only six parentheses, determine if the parentheses in the string are in the correct order. Six types of parentheses include parentheses (), brackets [], curly braces {}
4. Method of solving Problems 1
One way is to use the linked list to record the parentheses that were encountered, and each time you find the parentheses, check that the last parenthesis you added matches the currently found brace.
The Java code is as follows:
import java.util.linkedlist;/** * @ function Description: leetcode 20 - valid parentheses * @ Developer: tsybius2014 * @ Development Date: November 8, 2015 */public class solution { /** * reasonable combination of brackets * @param s * @return */ public boolean isvalid (string s) { linkedlist<character> linkedlist = new LinkedList<Character> (); for (Char ch : s.tochararray ()) { if (ch == ' (' | | ch == ' [' | | ch == ' {') { linkedlist.add (CH); } else if (ch == ') ') { if (Linkedlist.isempty () | | linkedlist.getlast () != ' (') { return false; } else { Linkedlist.removelast (); } } else if (ch == '] ') { if (Linkedlist.isempty () | | linkedlist.getlast () != ' [') { return false; } else { Linkedlist.removelast (); } } else if (ch == '} ') { if (Linkedlist.isempty () | | linkedlist.getlast () != ' {') { return false; } else { Linkedlist.removelast (); } } else { return false; } } Return linkedlist.isempty ();nbsp; }}
The
Alternative is to use stacks to handle the same way as the previous list:
import java.util.stack;/** * @ function Description: leetcode 20 - valid parentheses * @ Developer: tsybius2014 * @ Development Date: November 8, 2015 */public class Solution { /** * reasonable combination of brackets * @param s * @return */ public boolean isvalid (string s) { Stack<Character> stack = new Stack<character> (); for (Char ch : s.tochararray ()) { if (ch == ' (') { &nbSp; stack.push (') '); } else if (ch == ' [') { stack.push ('] '); } else if (ch == ' {') { stack.push ('} '); } else if (Stack.isempty () | | stack.pop () != ch) { return false; } } &Nbsp; return stack.isempty (); }}
5. Method of solving Problems 2
After looking at the discussion area, we also found another way to solve the problem, that is, using the Replace method under the string class to eliminate the adjacent positive and negative brackets, and finally can not be eliminated, see if there are residual characters in the string. This method is relatively simple to implement, but the efficiency is much lower than the two methods written above.
The Java code is as follows:
/** * @ function Description: leetcode 20 - valid parentheses * @ Developer: Tsybius2014 * @ Development Time: November 8, 2015 */public class Solution { /** * reasonable combination of brackets * @param s * @return */ public Boolean isvalid (string s) { int length = s.length (); String stemp; do { stemp = s; s = s.replace ("()", ""); &nBsp;s = s.replace ("[]", ""); s = s.replace ("{}", ""); length = s.length (); } while ( Length == s.length () && !stemp.equals (s)); return length == 0; }}
END
Leetcode:valid parentheses-A reasonable pair of parentheses