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?
这道题的难度定义为Easy,知道思路解决起来确实简单,但是要分析出思路并不容易。
题目要求出有多少种爬梯子的办法,并且规定每次只能爬一级阶梯或者两级解题,如此一来我们可以知道最后一步不是爬了一级阶梯就是爬了两级解题,那么问题就可以被分解了。
爬n级阶梯的种数 = 最后一步爬一级阶梯种数+最后一步趴两级阶梯的种数 = 爬n-1级阶梯的种数 + 爬n-2级阶梯的种数
————————————————————————————————————————————————————————
func climbStairs(n int) int {
if (n == 0 || n == 1) {
return n
}
res := make([]int,n + 1)
res[0] = 1;
res[1] = 1;
for i := 2; i <= n; i++ {
res[i] = res[i-1] + res[i-2]
}
return res[n]
}