[LeetCode] 70. Climbing Stairs

49人阅读 评论(1) 收藏 举报
分类:

题:https://leetcode.com/problems/climbing-stairs/description/

题目

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: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

思路

动态规划,DAG的路径,整体最大包含局部最大,在这道题上都有展现。
设dp(n)为 到n的最大中路线。
则状态转移方程为dp(n) = dp(n-1) + dp(n-2)

Code

class Solution {
public:
    int dp(int i ,int a[]){
        if(a[i] != -1)  return a[i];
        return a[i] = dp(i-1,a) + dp(i-2,a);
    }
    int climbStairs(int n) {
        if(n<=2)    return n;
        int a[n+1];
        for(int i = 0;i<=n;i++)
            a[i] = -1;
        a[1] = 1;
        a[2] = 2;
        return dp(n,a);  
    }
};
查看评论

leetcode之70. Climbing Stairs(C++解法 动态规划思想)

原题: You are climbing a stair case. It takes n steps to reach to the top.Each time you can either cl...
  • haimianxiaodao
  • haimianxiaodao
  • 2016-09-21 15:19:49
  • 487

leetcode 70 Climbing Stairs

Climbing Stairs                       You are climbing a stair case. It takes n steps to reach t...
  • wangyaninglm
  • wangyaninglm
  • 2015-04-29 17:46:09
  • 2859

【leetcode c++】70 Climbing Stairs

Climbing Stairs You are climbing a stair case. It takes nsteps to reach to the top. Each time you ...
  • hqq39
  • hqq39
  • 2015-06-30 12:31:56
  • 283

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...
  • fly_yr
  • fly_yr
  • 2015-08-19 21:54:20
  • 2971

(Java)LeetCode-70. Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cli...
  • u012848330
  • u012848330
  • 2016-11-06 16:01:21
  • 230

LeetCode 70 Climbing Stairs(Python详解及实现)

【题目】 You are climbing a stair case. It takes nsteps to reach to the top.   Each time you can eith...
  • yangjingjing9
  • yangjingjing9
  • 2017-08-05 12:56:00
  • 388

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...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2015-10-07 13:00:01
  • 2487

70. Climbing Stairs [easy] (Python)

题目链接https://leetcode.com/problems/climbing-stairs/题目原文 You are climbing a stair case. It takes n s...
  • coder_orz
  • coder_orz
  • 2016-05-26 13:10:58
  • 1727

LeetCode 70 — Climbing Stairs(C++ Java Python)

题目:http://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It takes n steps...
  • dragon_dream
  • dragon_dream
  • 2014-03-20 22:17:03
  • 3047

LeetCode-70-Climbing Stairs(动态规划)-Easy

1. 题意理解 每次可以上1个台阶或2个台阶,如果要上n个台阶,有多少种走法? 2. 题目分析: 1. 动态规划; 2. 使用递归方法会超时:climbStairs(n)=climbS...
  • eddy_liu
  • eddy_liu
  • 2016-03-19 13:17:05
  • 482
    个人资料
    等级:
    访问量: 2万+
    积分: 735
    排名: 7万+
    最新评论