1

Left:只要我们没有用完所有的左括号,我们总是可以插入一个左括号。右:只要不会导致语法错误,我们可以插入右括号。我们什么时候会收到语法错误

public class parentheses {

    public static void printPar(int l, int r, char[] str, int count){  //Use recursion method to 
                                                                       // print the parentheses
        if(l == 0 && r == 0){     //if there are no parentheses available, print them out  
            System.out.println(str); //Print out the parentheses
        }

        else{
            if(l > 0){    // try a left paren, if there are some available
                str[count] = '(';
                printPar(l - 1, r, str, count + 1); //Recursion
            }
            if(r > 0){   // try a right paren, if there are some available
                str[count] = ')';
                printPar(l, r - 1, str, count + 1);  //Recursion
            }
        }
    }

    public static void printPar(int count){
        char[] str = new char[count*2];   // Create a char array to store the parentheses
        printPar(count,count,str,0);      //call the printPar method, the parameters are the left,
                                            //the right parentheses, the array to store the 
                                            //parenthese, and the counter
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        printPar(2);  //

    }

}

结果应该是:

(())
()()

但我得到的是:

(())
()()
())(
)(()
)()(
))((
4

8 回答 8

4
import java.util.ArrayList;
import java.util.List;


public class parentheses {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(prent(2));
    }

    public static List<String> prent(int n) {
        List<String> l = new ArrayList<String>();
        if(n==1) {
            l.add("()");
            return l;
        }
        for(String st : prent(n-1)) {
            l.add(st+"()");
            l.add("("+st+")");
            if(!(st+"()").equals("()"+st)) {
                l.add("()"+st);
            }
        }
        return l;
    }

}
于 2013-10-01T10:17:50.767 回答
1

试试这个代码。测试和工作正常。

 public class ParanthesisCombination {
     public static void main(String[] args) {
          printParenthesis(3);
     }
     static void printParenthesis(int n){
        printParenthesis("",n,n);       
     }

     static void printParenthesis(String s,int open,int close){
         if(open>close)
            return;
         if(open == 0 && close == 0){
             System.out.println(s);
             return;
         }
         if(open < 0 || close<0)
             return;

         printParenthesis(s + '{',open-1,close);
         printParenthesis(s + '}',open,close-1);
     }
}
于 2013-01-09T04:53:12.433 回答
1
    private static void printA(int open, int close, int max, String out) {
        if(open==close && close==max){
            st.add(out);
            System.out.println(out);
        } else {
            if(open+1<=max){
                printA(open+1, close, max, out+"(");
            }
            if(open>close && close+1<=max){
                printA(open, close+1, max, out+")");
            }
        }
    }
    public static  ArrayList<String>st = new ArrayList<String>();
    public static void main(String[] args) {
        //2 is maximum open/close parenthese
        //i save the output in st(arraylist), 
        printA(0,0,2,"");
    }
于 2013-01-09T04:55:28.063 回答
0
public class parentheses {
    public static void printPar(int l, int r, char[] str, int count){  //Use recursion method to 
                                                               // print the parentheses

    if(l == 0 && r == 0){     //if there are no parentheses available, print them out  
        System.out.println(str); //Print out the parentheses
    }

    else{
        if(l > 0){    // try a left paren, if there are some available
            str[count] = '(';
            printPar(l - 1, r, str, count + 1); //Recursion
        }
        // Add constraint that a parenthesis is open before inserting a closing paranthesis
        // hence, add l < r - meaning one 'l' is printed before going for an 'r'
        if(r > 0 && l < r){   // try a right paren, if there are some available
            str[count] = ')';
            printPar(l, r - 1, str, count + 1);  //Recursion
        }
    }
}

    public static void printPar(int count){
        char[] str = new char[count*2];   // Create a char array to store the parentheses
        printPar(count,count,str,0);      //call the printPar method, the parameters are the left,
                                    //the right parentheses, the array to store the 
                                    //parenthese, and the counter
    }
    public static void main(String[] args) {
        printPar(2);  //
    }

}
于 2013-05-07T00:19:16.983 回答
0

我认为问题在于,在您的代码的任何部分中,您都没有强制执行“无 sintax 错误”部分。

您应该更改if(r > 0){...}forif(r > l){...}以执行该规则;如果没有至少 1 个左括号仍然“打开”,则不应打印右括号。

于 2013-01-09T04:53:44.647 回答
0

让我们假设给定的总对 - 3 保持有效,您只需要在开始时使用左大括号,在结束时需要一个右大括号,这意味着(2 对的组合)。

所以2对中的总有效+无效组合= 3 * 2 = 6..这些没有重复..认为'('是0,')'是1。

0000 1000 0100 1100 0010 1010 0110 1110 0001 1001 0101 1101 0011 1011 0111 1111

只能有 6 种可能的组合具有 2 个 0 和 2 个 1 .. 因此 2 对总组合 = 6 因此 3 对总有效组合 = 6

于 2013-05-03T02:25:57.043 回答
0
public class parentheses {
    public static void printPar(int l, int r, char[] str, int count){
          if(i<0 || r<l) return;
          ...
    }
}

你只是失去了结束条件。希望这能有所帮助。

于 2014-01-29T15:55:01.590 回答
0
public class ValidParenthesisCombi {
    public static void main(String[] args) {
        final int noOfBraces = 3;
        print(noOfBraces);
    }

    public static void print(int n){
        print("",n,n);
    }
    public static void print( String s, int open, int close){           
        if(open > close){
            return;
        }
        if((open==0)&&(close==0)){
            System.out.println(s);
        }else{
            if(open > 0){
                print(s+"{",open-1, close);
            }if(close > 0){
                print(s+"}",open, close-1);
            }
        }
    }
}
于 2016-12-30T07:27:00.227 回答