【一天一道LeetCode】#70. Climbing Stairs

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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总共有多少种不同的跳法。

很典型的动态规划问题,状态转移方程为,dp[n] = dp[n-1]+dp[n-2]。

class Solution {
public:
    int climbStairs(int n) {
        int dp[n];//纪录跳到i的不同路径的个数
        if(n==0) return 1;//0的时候返回1
        if(n==1) return 1;
        dp[0] = 1;
        dp[1] = 1;
        for(int i = 2 ; i <= n ; i++)
        {
            dp[i] = dp[i-1]+dp[i-2];//按状态转移方程进行计算
        }
        return dp[n];//返回跳到n的次数
    }
};
时间: 2024-08-13 10:28:50

【一天一道LeetCode】#70. 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 70 Climbing Stairs(爬楼梯)(动态规划)(*)

翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 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? 分析 动态规划基础题,首先设置3个变量用于

动态规划系列 Leetcode 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? Note: Given n will be a positive integer. Example 1: Input: 2 Output: 2 Explanation:

leetcode 70 Climbing Stairs ----- java

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[i] = dp[i-2]+dp[i-1] public class Solution { public int clim

[leetcode] 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? Note: Given n will be a positive integer. 1.递归(超时) public int climbStairs(int n) { i

[LeetCode] 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? 解题思路: 动态规划DP(Dynamic Programming)入门题. state: dp[i] 表示爬到第i个楼梯的所有方法的和function: dp[i] =

[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