Climbing Stairs
思路一: 动态规划,假设爬上第 n 阶楼梯,完全分类只可能存在两种情况
- 在 n-1 楼梯处直接一步上来
- 在 n-2 楼梯处直接两步上来
所以 爬上第 n 阶楼梯的方法: f(n) = f(n-1) + f(n+1)
public int climbStairs(int n) {
if (n == 1 || n == 2) return n;
int result = 0;
int stepOne = 1;
int stepTwo = 2;
for (int i = 3; i <= n; i++) {
result = stepOne + stepTwo;
stepOne = stepTwo;
stepTwo = result;
}
return result;
}
标签:int,70,easy,上来,楼梯,leetcode
From: https://www.cnblogs.com/iyiluo/p/16786936.html