Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “无法理解”;括号对的组合;算法?_Java_Algorithm - Fatal编程技术网

Java “无法理解”;括号对的组合;算法?

Java “无法理解”;括号对的组合;算法?,java,algorithm,Java,Algorithm,任务:查找包含N个左括号和N个右括号的正则括号表达式的数目。N是从键盘输入的 我找到了这个求解算法,并试图理解它 public static void addParen(ArrayList<String> list, int leftRem, int rightRem, char[] str, int count) { if (leftRem < 0 || rightRem < leftRem) return; // some state if (lef

任务:查找包含N个左括号和N个右括号的正则括号表达式的数目。N是从键盘输入的

我找到了这个求解算法,并试图理解它

public static void addParen(ArrayList<String> list, int leftRem, int rightRem, char[] str, int count) {
    if (leftRem < 0 || rightRem < leftRem) return; // some state

    if (leftRem == 0 && rightRem == 0) { /* no additional left parentheses */
        String s = String.copyValueOf(str);
        list.add(s);
    } else {
        /* Add left parenthesis if there are parentheses of any kind */
        if (leftRem > 0) {
            str[count] = '(';
            addParen(list, leftRem - 1, rightRem, str, count + 1);
        }

        /* Add a right parenthesis if the expression is true */
        if (rightRem > leftRem) {
            str[count] = ')';
            addParen(list, leftRem, rightRem - 1, str, count + 1);
        }
    }
}
public static ArrayList<String> generateParens(int count) {
    char[] str = new char[count * 2];
    ArrayList<String> list = new ArrayList<String>();
    addParen(list, count, count, str, 0);
    return list;
}

嗯,当然这是编程课上的一项热门任务:)而且你会在网上找到很多用多种语言编写的解决方案,比如。事实上,这件很难看。也许你会深入研究,想出一些更能抚摸编码员心灵的东西:)

publicstaticlist-generateParenthesis(intn){
ArrayList结果=新建ArrayList();
ArrayList diff=新的ArrayList();
结果:添加(“”);
差加(0);
对于(int i=0;i<2*n;i++){
ArrayList temp1=新的ArrayList();
ArrayList temp2=新的ArrayList();
对于(int j=0;j0&&i<2*n-1 | k==1&&i==2*n-1){
temp1.添加(s+”);
temp2.add(k-1);
}
}
结果=新阵列列表(temp1);
diff=新阵列列表(temp2);
}
返回结果;
}

请注意,您的任务是计算括号的有效序列,而不是输出它们。那么,让我们想想有多少变体可以构成一个长度为N(对)的有效序列(表示为s(N)):

因此,总体公式:

S(N) = Sum[S(N-M-1)*S(M)] for M=0..N-1   (with S(0)=1)
只需将数组/列表从较小的值填充到较大的值即可

例如:

S(3) = S(2)*S(0)+S(1)*S(1)+S(0)*S(2)=2+1+2=5

请注意,简明公式确实存在,但我认为您的讲师希望得到一个具有某种逻辑结论的解决方案

请让我们知道该算法将产生什么样的结果。任务是什么?iuliq我也在括号中迷失了方向;(:Dwhat if count=4?目前您的任务尚未完成表达式的数量或表达式列表?(注意,数量很快就会变大)谢谢!!您的解决方案更好…但对我来说也很难!:D
You can get any valid sequence of length N-1 and add () pair after it  
You can get any valid sequence of length N-2 and add opening parenthesis,
valid sequence of length 1, and closing parenthesis  
...   
You can get any valid sequence of length N-1-M and add opening parenthesis, 
any valid sequence of length M, and closing parenthesis  
...  
You can make opening parenthesis, add any valid sequence of length N-1, 
and closing parenthesis
S(N) = Sum[S(N-M-1)*S(M)] for M=0..N-1   (with S(0)=1)
S(3) = S(2)*S(0)+S(1)*S(1)+S(0)*S(2)=2+1+2=5