其他分享
首页 > 其他分享> > 爬楼梯问题

爬楼梯问题

作者:互联网

假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

注意:给定 n 是一个正整数。

class Solution {
public int climbStairs(int n) {
if(n < 3){
return n;
}
int[] nums = new int[n + 1];
nums[1] = 1;
nums[2] = 2;
for(int i = 3; i <= n; i++){
nums[i] = nums[i - 2] + nums[i - 1];//为什么是前两种的和--(先可以试下硬数就可以发现规律并且总结出来)假如你要上5楼你就有四楼上一阶和3楼上两阶的两种
}
return nums[n];
}
}

 

标签:楼顶,爬楼梯,nums,int,问题,return,楼上
来源: https://www.cnblogs.com/lhniubi/p/14525852.html