这个想法是,如果我在某个楼梯我可以走一步或两步,所以如果我在楼梯3我可以走下去1 1 1或2 1例如。我的代码应该打印所有的可能性。我得到的错误是我无法将add函数转换为数组(因为add方法是一个布尔值)。这个算法有什么问题?在递归算法中使用数组来寻找组合
public class Stairs {
public static void staircase (int height ){
ArrayList<Integer> Array = null;
explore (height,Array);
}
public static void explore(int objheight,ArrayList<Integer>Array){
int intialheight = 0;
if (intialheight == objheight){
Array.toString();
}
else{ if (objheight > intialheight){
explore(objheight-2,Array.add(2));
explore(objheight-1,Array.add(1));
}
}
您的意见我得到一个空的输出 进口java.lang.reflect.Array中后
; import java.util.ArrayList;
public class Stairs {
public static void staircase (int height ){
ArrayList<Integer> Array = new ArrayList<Integer>();
explore (height,Array);
}
public static void explore(int objheight,ArrayList<Integer>Array){
int intialheight = 0;
if (intialheight == objheight){
Array.toString();
}
else{ if (objheight > intialheight){
Array.add(2);
explore(objheight-2,Array);
Array.add(1);
explore(objheight-1,Array);
}
}}
public static void main (String args[]){
staircase(3);
}
}
什么是 “add函数转换为数组” 是什么意思?我很肯定你会得到一个NullPointerException。 – Henry