您的位置:首页 > 大数据 > 人工智能

【Leetcode】Climbing Stairs

2016-06-04 20:15 375 查看
题目链接: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数组表示到每一步为止,有多少种方法。

算法:

[java] view
plain copy

 





public int climbStairs(int n) {  

    int[] data = new int[n + 3];  

    data[1] = 1;  

    data[2] = 2;  

    for (int i = 3; i <= n; i++) {  

        data[i] = data[i - 2] + data[i - 1];  

    }  

    return data
;  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: