You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
题目链接:https://leetcode.com/problems/climbing-stairs/
题目分析:斐波那契数列
public class Solution {
public int climbStairs(int n) {
int[] fib = new int[n + 1];
fib[0] = 1;
fib[1] = 1;
for (int i = 2; i <= n; i ++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}
}

本文探讨了一个经典的编程面试题——爬楼梯问题,并通过使用斐波那契数列来解决该问题。文章提供了一种简洁的Java实现方案,通过递推的方式计算出所有可能的爬楼方式。
345

被折叠的 条评论
为什么被折叠?



