leetcode 70. Climbing Stairs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 70. Climbing Stairs相关的知识,希望对你有一定的参考价值。

leetcode 70. 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?

Note: Given n will be a positive integer.

 

本题考虑动态规划,每一阶台阶只能从它的前一个台阶来或者从前两个台阶来,那么只需知道前一个台阶和前两个台阶能有几种走法就ok了,

第一个台阶是1中,第二个台阶是两种,注意只有一个台阶的特殊情况

 

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

 

以上是关于leetcode 70. Climbing Stairs的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 70. Climbing Stairs

Leetcode-70 Climbing Stairs

Leetcode 70 Climbing Stairs

[Leetcode 70]: Climbing Stairs

[leetcode]70.Climbing Stairs

LeetCode 70. Climbing Stairs