Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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/3/arrays/14.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_Arrays_String_Permutation - Fatal编程技术网

Java 如何将所有数组置换迭代返回到二维数组中?

Java 如何将所有数组置换迭代返回到二维数组中?,java,arrays,string,permutation,Java,Arrays,String,Permutation,我正在尝试编写一个程序,它将遍历字符串数组的所有可能排列,并返回一个包含所有排列的二维数组。具体地说,我尝试使用长度为4的字符串数组返回一个包含24行和4列的二维数组 我只找到了迭代打印字符串的方法,但没有在数组中使用它们。我也找到了递归的方法,但是它们不起作用,因为我正在和其他人一起使用这段代码,递归函数要困难得多 对于我希望代码执行的操作,我知道标题应该是: public class Permutation { public String[][] arrayPermutation(

我正在尝试编写一个程序,它将遍历字符串数组的所有可能排列,并返回一个包含所有排列的二维数组。具体地说,我尝试使用长度为4的字符串数组返回一个包含24行和4列的二维数组

我只找到了迭代打印字符串的方法,但没有在数组中使用它们。我也找到了递归的方法,但是它们不起作用,因为我正在和其他人一起使用这段代码,递归函数要困难得多

对于我希望代码执行的操作,我知道标题应该是:

public class Permutation
{
     public String[][] arrayPermutation(String[] str)
     {
          //code to return 2D array
     }
}
//我尝试将递归方法与heap的算法结合使用,但它的参数非常复杂


我对编程非常陌生,非常感谢您的帮助。

您的排列问题基本上只是一个索引排列问题。 如果可以在所有可能的变体中对数字从0到n-1进行排序,那么可以将它们用作输入数组的索引,并简单地复制字符串。下面的算法不是最优的,但它足够形象地解释和迭代实现

public static String[][] getAllPermutations(String[] str) {
    LinkedList<Integer> current = new LinkedList<>();
    LinkedList<Integer[]> permutations = new LinkedList<>();

    int length = str.length;
    current.add(-1);

    while (!current.isEmpty()) {
        // increment from the last position.
        int position = Integer.MAX_VALUE;
        position = getNextUnused(current, current.pop() + 1);
        while (position >= length && !current.isEmpty()) {
            position = getNextUnused(current, current.pop() + 1);
        }
        if (position < length) {
            current.push(position);
        } else {
            break;
        }

        // fill with all available indexes.
        while (current.size() < length) {
            // find first unused index.
            int unused = getNextUnused(current, 0);

            current.push(unused);
        }
        // record result row.
        permutations.add(current.toArray(new Integer[0]));
    }

    // select the right String, based on the index-permutation done before.
    int numPermutations = permutations.size();
    String[][] result = new String[numPermutations][length];
    for (int i = 0; i < numPermutations; ++i) {
        Integer[] indexes = permutations.get(i);
        String[] row = new String[length];
        for (int d = 0; d < length; ++d) {
            row[d] = str[indexes[d]];
        }
        result[i] = row;
    }

    return result;
}

public static int getNextUnused(LinkedList<Integer> used, Integer current) {
    int unused = current != null ? current : 0;
    while (used.contains(unused)) {
        ++unused;
    }
    return unused;
}
publicstaticstring[]getAllPermutations(String[]str){
LinkedList current=新建LinkedList();
LinkedList排列=新建LinkedList();
int-length=str.length;
当前。添加(-1);
而(!current.isEmpty()){
//从最后一个位置开始递增。
int位置=整数最大值;
position=getNextUnused(current,current.pop()+1);
while(位置>=长度&&!current.isEmpty()){
position=getNextUnused(current,current.pop()+1);
}
if(位置<长度){
当前推(位置);
}否则{
打破
}
//填充所有可用的索引。
while(当前.size()
getAllPermutations方法组织在初始化部分、收集所有置换(数值)的循环中,最后将找到的索引置换转换为字符串置换

因为从int到String的转换很简单,所以我只解释集合部分。只要表示没有完全耗尽或从内部终止,循环就会迭代

首先,我们增加表示(
current
)。为此,我们取最后一个“数字”并将其递增到下一个自由值。然后我们弹出,如果我们超过长度,看下一个数字(并增加它)。我们继续这样做,直到达到一个合法值(长度以下的值)

然后,我们用所有剩余的数字填充剩余的数字。完成后,我们将当前表示形式存储到数组列表中

此算法在运行时方面不是最优的!堆速度更快。但是,迭代地实现堆需要一个非平凡的堆栈,这对于实现/解释来说很麻烦。