LeetCode | 70. Climbing Stairs

来源:互联网 发布:如何ping ip 的端口 编辑:程序博客网 时间:2024/06/05 15:26

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.

思路:记忆型动规,否则超时

class Solution {public:    int vis[1000] = {};    int climbStairs(int n) {        if(n == 1)            return 1;        if(n == 2)            return 2;        int adder1 = 0, adder2 = 0;        if(vis[n-1] != 0)            adder1 = vis[n-1];        else        {            adder1 = climbStairs(n-1);            vis[n-1] = adder1;        }        if(vis[n-2] != 0)            adder2 = vis[n-2];        else        {            adder2 = climbStairs(n-2);            vis[n-2] = adder2;        }        return adder1 + adder2;    }};