题目链接:https://leetcode.com/problems/climbing-stairs/

题目:

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?

思路:

上一步怎么走的跟下一步都的没关系。用data数组表示到每一步为止,有多少种方法。

算法:


 
 
1. public int climbStairs(int n) {  
2. int[] data = new int[n + 3];  
3. 1] = 1;  
4. 2] = 2;  
5. for (int i = 3; i <= n; i++) {  
6. 2] + data[i - 1];  
7.     }  
8. return data[n];  
9. }