4
你好我想这个代码非递归我怎么能做到这一点?递归代码非循环递归
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);
}
}
}
它从特定数字生成所有组合。
到目前为止,您*尝试了什么?顺便说一句:我们在谈论什么编程语言?请将它添加到标签! –
为什么你想要它是非递归的?递归是检查所有组合的一种通用方法。如果你的情况对于{0,1,2,3}非常具体,那么应该有一个三或四个循环的方法。 – nawfal
因为是一个学校作业 – user2897452