Leecode no.70 爬楼梯

package leecode;

/**
*
* 70. 爬楼梯
* 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
*
* 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
*
* 注意:给定 n 是一个正整数。
*
* @author Tang
* @date 2021/9/15
*/
public class ClimbStairs {

/**
* 动态规划
* n :第n层楼梯
* f(n) :爬到第n层楼梯所可能的方法数
* f(n) = f(n-1) + f(n-2)
* base: f(1) = 1 f(2) = 2
*
* @param n
* @return
*/
public int climbStairs(int n) {
if(n == 0) {
return 0;
}
// n1 = f(n-1)
int n1 = 2;
// n2 = f(n-2)
int n2 = 1;
int result = 0;
for(int i = 1; i <= n; i++) {
if(i == 1) {
result = 1;
continue;
}
if(i == 2) {
result = 2;
continue;
}
result = n1 + n2;
n2 = n1;
n1 = result;
}
return result;
}


public static void main(String[] args) {


}


}
原文地址:https://www.cnblogs.com/ttaall/p/15271909.html