[LeetCode]70. Climbing Stairs

来源:互联网 发布:微信站街营销软件 编辑:程序博客网 时间:2024/03/28 16:39

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?

思路:动态规划,每增加一级台阶,最后一步有两种走法,一个台阶or两个台阶

递推公式为 store[i] = store[i-1] + store[i-2];

代码如下:

   public int climbStairs(int n) {    if(n < 0)        return 0;    if(n == 1)        return 1;    int[] store = new int[n];    store[0] = 1;    store[1] = 2;    for(int i = 2; i < n; ++i)        store[i] = store[i-1] + store[i-2];    return store[n-1];    }


0 0
原创粉丝点击