[Lintcode Easy] Valid parentheses

Source: Internet
Author: User
Tags lintcode

Valid parentheses

Given A string containing just the characters,, ‘(‘, ‘)‘ ‘{‘ , and ‘}‘ ‘[‘ ‘]‘ , determine if the input string is valid .

Example

The brackets must close in the correct order, and is all valid but and is not "()" "()[]{}" "(]" "([)]" .

////

The substring () method method in Java string is as follows: public string substring (int beginindex, int endIndex) The first int is the starting index, corresponding to the start position in the string number, The second is the cutoff index position, corresponding to the end position in string 1, the length of the string obtained is: Endindex-beginindex;2, starting from Beginindex, to the end of the EndIndex, starting from 0 number, The word such as: "Hamburger" is not included in the Endindex position. SUBSTRING (4, 8) returns "Urge" "smiles". Substring (1, 5) returns "Mile"////////

 Public classSolution {/**     * @params A String *@returnwhether the string is a valid parentheses*/     Public Booleanisvalidparentheses (String s) {if(s==NULL)return false; if(S.length ()%2!=0)return false; //Write Your code here        Char[] c=S.tochararray ();  for(intI=1;i<s.length (); i++)        {            Booleanr1= c[i-1]== ' (' && c[i]== ') '; Booleanr2= c[i-1]== ' {' && c[i]== '} '; Booleanr3= c[i-1]== ' [' && c[i]== ']; if(r1| | r2| |R3) {String S1=s.substring (0,i-1) +s.substring (i+1); if(S1.length () ==0)return true; Else returnisvalidparentheses (S1); }        }        return false; }}

[Lintcode Easy] Valid 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.