Leetcode:valid parentheses-A reasonable pair of parentheses

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.