欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

面试问题 有50级台阶,你每次可以走一阶或两阶,有多少种走法

程序员文章站 2024-01-13 11:55:40
...
有50级台阶,你每次可以走一阶或两阶,有多少种走法
程序列出所有可能

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

public class GoStairs {

	private List<Integer> stepStlye = new ArrayList<Integer>();
	public static final int STAIRS_COUNT = 10;

	public void goStairs(int leftStairsCount) {
		if (leftStairsCount - 1 >= 0) {
			stepStlye.add(1);
			leftStairsCount = leftStairsCount - 1;
			goStairs(leftStairsCount);
			leftStairsCount = leftStairsCount + 1;
			stepStlye.remove(stepStlye.size() - 1);
		}
		if (leftStairsCount - 2 >= 0) {
			stepStlye.add(2);
			leftStairsCount = leftStairsCount - 2;
			goStairs(leftStairsCount);
			leftStairsCount = leftStairsCount + 2;
			stepStlye.remove(stepStlye.size() - 1);
		}
		if (leftStairsCount == 0) {
			System.out.println(Arrays.toString(stepStlye.toArray(new Integer[0])));
			return;
		}
	}

	public static void main(String[] args) {
		GoStairs ladder = new GoStairs();
		ladder.goStairs(STAIRS_COUNT);
	}
}

相关标签: 面试 java