[LeetCode] 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?

 

Hide Tags
 Dynamic Programming
Have you met this question in a real interview? 
Yes
 
No
 

Discuss

 

思路:动态规划+斐波那契数列,最简单的dp

class Solution {
    public:
        int climbStairs(int n) {

            vector<int> f(n+1, 0); 
            f[0] = 1;
            f[1] = 1;

            for(int i = 2; i <= n; i++)
            {   
                f[i] = f[i-1] + f[i-2];
            }   

            return f[n];
        }   
};

 

 

posted @ 2015-03-10 14:07  穆穆兔兔  阅读(133)  评论(0编辑  收藏  举报