leetcode No70. Climbing Stairs

Question:

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?

爬楼梯,一次只能爬1阶或者2阶,问总共有多少种结果。

Algorithm:

Dynamic Programming动态规划,s[i]=s[i-1]+s[i-2]

Accepted Code:

class Solution {
public:
    int climbStairs(int n) {
        if(n==0)return 0;
        else if(n==1)return 1;
        else if(n==2)return 2;
        vector<int> res(n,0);
        res[0]=1;
        res[1]=2;
        for(int i=2;i<n;i++)
            res[i]=res[i-1]+res[i-2];
        return res[n-1];
    }
};

时间: 2024-08-30 05:40:34

leetcode No70. Climbing Stairs的相关文章

[LeetCode OJ]-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? 一共有n阶楼梯,每次只能爬1阶或2阶,问爬到顶端一共有多少种方法 方法一: 利用二叉树的思想,对于n=3时有3种方法,有几个叶子节点便有几种方法 1 void clim

【LeetCode】Climbing Stairs

Set集合的配置 数据表的创建:表关系一个员工拥有多个身份 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR(20) default NULL, last_name VARCHAR(20) default NULL, salary INT default NULL, PRIMARY KEY (id) ); create table CERTIFICATE ( id INT NOT NULL aut

Leetcode 动态规划 Climbing Stairs

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Climbing Stairs Total Accepted: 13319 Total Submissions: 40778 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 distinc

LeetCode | 0070. Climbing Stairs爬楼梯【Python】

LeetCode 0070. Climbing Stairs爬楼梯[Easy][Python][动态规划] Problem LeetCode 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

[LeetCode][JavaScript]Climbing Stairs

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? https://leetcode.com/problems/climbing-stairs/ 直接递归超时了,要动态规划. 打印前几个数

leetCode 70. Climbing Stairs | 动态规划

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? 思考: top steps 1 1 2 2 3 3 4 5 5 8 6 13 ... ... 从上面的分析可以看出,f(top)

leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

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? 思路:题目也比较简单,类似斐波那契. 代码如下: public class Solution { public int climbSta

【LeetCode】Climbing Stairs (2 solutions)

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? 这是最简单的DP了,递推公式为f(n)=f(n-1)+f(n-2) 因此建立vector存放中间结果即可. class Solution

[leetcode] 14. Climbing Stairs

这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了.题目如下: 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? 这个基本就可以很简单的做成递归.如果楼层是