1

左:左括弧をすべて使い切っていない限り、いつでも左括弧を挿入できます。右:構文エラーが発生しない限り、右のパレンを挿入できます。構文エラーはいつ発生しますか

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

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

問題は、コードのどの部分でも「シンタックスエラーなし」の部分を強制していないことだと思います。

そのルールを適用するにif(r > 0){...}は、 forを変更する必要があります。if(r > l){...}少なくとも 1 つの左括弧がまだ「開いている」場合、右括弧は出力されません。

于 2013-01-09T04:53:44.647 に答える
0

与えられた合計ペアを仮定しましょう - 3 を有効に保つには、開始時の開きブレースと終了時の 1 つの閉じブレースのみが必要です (つまり、2 ペアの組み合わせ)。

したがって、2 組のうちの有効な組み合わせと無効な組み合わせの合計 = 3*2 = 6.. これらには繰り返しはありません.. 「(」は 0、「)」は 1 と考えてください。

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

2 つの 0 と 2 つの 1 を持つ可能な組み合わせは 6 つだけです。したがって、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 に答える