Similar topics are thought to be solved by recursive methods.
Solution: The legal sequence of parentheses, the number of open parentheses already inserted is greater than the number of closing parentheses.
(1) Inserting the opening parenthesis: the remaining brackets, and the left parenthesis;
(2) Insert closing parenthesis: The remaining brackets, the number of closing brackets is greater than the number of left parenthesis;
Code:
public class Parentheses {public
static void Printpar (int l, int r, char[] str, int count) {
if (l<0 | | r<l) return;
if (l==0&&r==0) {
System.out.println (str);
} else{
if (l>0) {
str[count]= ' (';
Printpar (L-1, R, str, count+1);
if (r>l) {
str[count]= ') ';
Printpar (l, r-1, str, count+1);
}} public static void Main (string[] args) {
int count = 4;
Char str[] = new char[count*2];
Printpar (count, Count, str, 0);
}
The results are as follows:
(((())))
((()()))
((())())
((()))()
(()(()))
(()()())
(()())()
(())(())
(())()()
()((()))
()(()())
()(())()
()()(())
()()()()