[Leetcode 70, easy] Climbing Stairs

来源:互联网 发布:vb用的是什么语言 编辑:程序博客网 时间:2024/05/16 11:42

Problem:

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?

Analysis:


Solutions:

C++:

    int climbStairs(int n) {        if(n == 0)            return n;        vector<int> different_ways;        different_ways.push_back(1);        for(int i = 1; i < n; ++i) {            if(i == 1)                different_ways.push_back(2);            else                different_ways.push_back(different_ways[i - 1] + different_ways[i - 2]);        }                return different_ways[n - 1];    }
Java:


Python:

0 0
原创粉丝点击