You are climbing a stair case. It takes n steps to reach to the top.spa
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?code
一共N階樓梯,一次只上1階或2階,求一共多少種上樓梯的方法。blog
動態規劃問題it
f(n)爲總數爲n個樓梯的走法總數class
f(n) = f(n - 1) (走一步)cli
+ f(n - 2) (走兩步)方法
int climbStairs(int n) { int count[n + 1]; count[0] = 0; count[1] = 1; count[2] = 2; if(n == 0 || n == 1) { return 1; } if(n == 2) { return 2; } for(int i = 3; i < n + 1; i++) { count[i] = count[i - 1] + count[i - 2]; } return count[n]; }