/**
* Function: Print all valid combinations of n pairs of parentheses (i.e. right and left brackets are paired correctly).
*/
Two methods:
Method One:
/** * Idea: Insert a pair of parentheses in the front of the parentheses or in each pair of parentheses. As for any other position, such as the end of the string, it will repeat with the previous situation. * Note: Before you put a string in the results list, you must check that the list has no duplicates. * @param remaining * @return */public static hashset<string> generateparens (int remaining) {hashset<string> Set=new hashset<string> (); if (remaining==0) Set.add ("");else{hashset<string> Prev=generateparens ( REMAINING-1); for (String Str:prev) {//parentheses insert for (int i=0;i<str.length (); i++) {if (Str.charat (i) = = ' (') {String s= Insertinside (str,i); Set.add (s);//before inserting elements, HashSet automatically checks for duplicates}}//parentheses first insert if (!set.contains ("()" +str) Set.add ("()" +str );}} return set;} /** * Insert inside each pair of parentheses * @param str * @param leftindex * @return */public static string Insertinside (String str,int leftindex) {str ing left=str.substring (0,leftindex+1);//parentheses are inserted after the opening parenthesis, so leftindex+1string right=str.substring (leftindex+1) is required; return Left+ "()" +right;}
Method Two:
/** * Idea: Construct a string from scratch to avoid repeating strings. Note the opening parenthesis and the closing parenthesis are added as long as the string is still valid. * Each recursive call has an index that points to a character in the string. Select opening parenthesis and closing parenthesis: * 1) Opening parenthesis: You can insert an opening parenthesis as long as the left parenthesis is not exhausted. * 2) Right parenthesis: You can insert a closing parenthesis (more than the left parenthesis, which is syntax error) as long as you do not cause a syntax error. * Therefore, just record the number of left and right brackets that are allowed to be inserted. If there is an opening parenthesis available, insert an opening parenthesis and then recursively. * If the parentheses are more than the left parenthesis (that is, the opening parenthesis in use is more than the right parenthesis), a closing parenthesis is inserted, and then recursively. * * Insert opening and closing parentheses at each index of the string, and never repeat index!!! * @param count * @return */public static arraylist<string> generateParens2 (int count) {arraylist<string> list =new arraylist<string> (); char[] Str=new char[count*2];addparens (list,count,count,str,0); return list;} public static void Addparens (arraylist<string> list, int leftrem, int rightrem, char[] str, int count) {//TODO Auto -generated method Stubif (leftrem<0| | Rightrem<leftrem) return;if (leftrem==0&&rightrem==0) {String s=string.copyvalueof (str); List.add (s);} if (leftrem>0) {str[count]= ' ('; Addparens (list, leftRem-1, Rightrem, str, count+1);} if (rightrem>leftrem) {str[count]= ') '; Addparens (list, Leftrem, rightRem-1, str, count+1);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
9.9 Recursive and dynamic Planning (vi)--print all valid combinations of n pairs of parentheses (i.e. right and left brackets paired correctly)