您好,我希望这段代码是非递归的,我该怎么做?
public class test {
public static void main(String[] args) {
int[] array = new int[]{0, 1, 2,3};
int size = 2;
int[] tmp = new int[size];
//Arrays.fill(tmp, -1);
generateCombinations(array, 0, 0, tmp);
}
private static void generateCombinations(int[] array, int start, int depth, int[] tmp) {
if (depth == tmp.length) {
for (int j = 0; j < depth; j++) {
System.out.print(array[tmp[j]]);
} System.out.println();
return;
}
for (int i = start; i < array.length; i++) {
tmp[depth] = i;
generateCombinations(array, i + 1, depth + 1, tmp);
}
}
}
它从特定数字生成所有组合。