Java 用递归方法的列表替换字符串参数

Java 用递归方法的列表替换字符串参数,java,string,list,recursion,permutation,Java,String,List,Recursion,Permutation,在过去的3个小时里,我一直在努力解决一个可能很简单的问题。我重写了一个类,并用列表替换了2个字符串参数。 问题是,在调用rekursive方法时,在第一个字符串参数中添加了1个字符。当参数的长度达到7时,它会打印出来。字符串的长度永远不会超过7。 我将其替换为整数列表,因为字符串仅由数字组成。 但是这个名单越来越长,我不知道为什么。我希望我把一切都解释清楚。如果没有,请问我 这个问题对你们来说可能非常容易回答 这是第一节课,很有效 package Uebung4; public cla

在过去的3个小时里,我一直在努力解决一个可能很简单的问题。我重写了一个类,并用列表替换了2个字符串参数。 问题是,在调用rekursive方法时,在第一个字符串参数中添加了1个字符。当参数的长度达到7时,它会打印出来。字符串的长度永远不会超过7。 我将其替换为整数列表,因为字符串仅由数字组成。 但是这个名单越来越长,我不知道为什么。我希望我把一切都解释清楚。如果没有,请问我

这个问题对你们来说可能非常容易回答

这是第一节课,很有效

    package Uebung4;

public class PermAll_Alt {

static int counter = 0;

private static void permutation(String word, String str) {

    int n = str.length();

    // System.out.println(str + " Str");
    // System.out.println(word + " word");

    if (n == 0) {

        if (

        (Integer.parseInt(word.substring(0, 1))) > (Integer.parseInt(word.substring(1, 2)))
                && (Integer.parseInt(word.substring(1, 2))) < (Integer.parseInt(word.substring(2, 3)))
                && (Integer.parseInt(word.substring(2, 3))) > (Integer.parseInt(word.substring(3, 4)))
                && (Integer.parseInt(word.substring(3, 4))) < (Integer.parseInt(word.substring(4, 5)))
                && (Integer.parseInt(word.substring(4, 5))) > (Integer.parseInt(word.substring(5, 6)))
                && (Integer.parseInt(word.substring(5, 6))) < (Integer.parseInt(word.substring(6, 7)))

        ) {
            // System.out.println(word);
            counter++;
        }

    } else {
        for (int i = 0; i < n; i++) {
            // System.out.println("Word: " +word+"\t str charat:
            // "+str.charAt(i));
            // System.out.println(word + str.charAt(i) + " \t combined");

            System.out.println("substr(0,i): " + str.substring(0, i) + " substr(i+1) " + str.substring(i + 1));
            permutation(word + str.charAt(i), str.substring(0, i) + str.substring(i + 1));
        }
    }
}

public static void main(String[] args) {
    permutation("", "1234567");
    System.out.println("Anzahl:  " + counter);
}
}
这是我编辑的课程:

package Uebung4;

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

public class PermAll {

static int counter = 0;

private static void permutation(List<Integer> wordList, List<Integer> lis) {

  //  List<Integer> wordList2 = cloneList(wordList);

    int n = lis.size();

    if (n == 0) {
        String word = "";
        for (Integer i : wordList) {
            word += i;
        }


        if ((Integer.parseInt(word.substring(0, 1))) > (Integer.parseInt(word.substring(1, 2)))
                && (Integer.parseInt(word.substring(1, 2))) < (Integer.parseInt(word.substring(2, 3)))
                && (Integer.parseInt(word.substring(2, 3))) > (Integer.parseInt(word.substring(3, 4)))
                && (Integer.parseInt(word.substring(3, 4))) < (Integer.parseInt(word.substring(4, 5)))
                && (Integer.parseInt(word.substring(4, 5))) > (Integer.parseInt(word.substring(5, 6)))
                && (Integer.parseInt(word.substring(5, 6))) < (Integer.parseInt(word.substring(6, 7)))

        ) {
            System.out.println(word);
            // convertToDU(word);
            counter++;
        }

    } else {

        for (int i = 0; i < n; i++) {
            List<Integer> tempLis = new ArrayList<>();

           //String tempString = "";

            for (int j = 0; j < i; j++) {
                tempLis.add(lis.get(j));
            }
            System.out.print("str.substr(0,i): " + tempLis+"\t");


            for (int k = i + 1; k < lis.size(); k++) {
                tempLis.add(lis.get(k));
                System.out.print(""+lis.get(k)+", ");
            }
            System.out.println(tempLis);

            // System.out.println("word "+wordList + "\t charat:
            // "+lis.get(i));
            wordList.add(lis.get(i));

          //  System.out.println(wordList + " \t kombiniert");

            permutation(wordList, tempLis);
            // permutation(word + lis.get(i),tempLis);
        }

    }
}

public static void main(String[] args) {

    List<Integer> list = new ArrayList<Integer>();

    int anzahl = 7;

    for (int i = 1; i <= anzahl; i++) {
        list.add(i);
    }

    String para = "";
    for (Integer i : list) {
        para += i;
    }

    List<Integer> abc = new ArrayList<>();

    permutation(abc, list);
    System.out.println("Anzahl:  " + counter);

}
}

下面是一个解决方案:我使用字符串args版本中执行递归调用的代码,并将逻辑复制到列表args版本:

        for (int i = 0; i < n; i++) {
            // create a copy of wordList
            List<Integer> permWordList = new ArrayList<Integer>(wordList);

            // equiv to "word + str.charAt(i)"
            permWordList.add(strLis.get(i));  

            // create a copy of lis
            List<Integer> permStrList = new ArrayList<Integer>(lis);

            // equiv to "str.substring(0, i) + str.substring(i + 1)"
            permStrList.remove(i);  

            permutation(permWordList, permStrList);
        }

看起来很好用。谢谢!你能给我解释一下,为什么我要复制这份名单吗?天才的想法是只删除一个列表中的元素i,而不是围绕i计数。我觉得没有看到这一点很愚蠢。你需要创建wordList和lis的副本,以便每次迭代都能得到一份新副本,否则,lis将变得越来越小。最后,请通过接受和/或向上投票来表示你的感谢。。。