翻译

你正在爬一个楼梯。

它须要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个变量用于转换:

int dp1 = 1, dp2 = 2, dpWay = 0;

依据题意,一次仅仅能是一步或两步。所以当n等于2时,有两种走法:1+1,2。

if (n <= 1) return dp1;
if (n == 2) return dp2;

从3開始,由于能够直接获得它的步数结果。所以直接写成:

while ((n--)-2) {
}

终于里面的变化方式为:

dpWay = dp1 + dp2;
dp1 = dp2;
dp2 = dpWay;

上一篇博客:LeetCode 206 Reverse Linked List(反转链表)(四步将递归改写成迭代)(*) ,介绍了怎样将递归改写成迭代,看过的童鞋应该会认为很easy的。那么这里再来转换一次:

int climbStairsIter(int n,  int dpWay,int dp1, int dp2) {
if (n <= 1) return dp1;
if (n == 2) return dp2;
if ((n--) - 2) {
dpWay = dp1 + dp2;
dp1 = dp2;
dp2 = dpWay;
return climbStairsIter(n, dpWay, dp1, dp2);
}
else return dpWay;
} int climbStairs(int n) {
return climbStairsIter(n, 0,1,2);
}

由于这里的參数涉及到运行前面顺序,所以不妨单独列出来了,只是这样看来略不简洁呐。

代码

class Solution {
public:
int climbStairs(int n) { int dp1 = 1, dp2 = 2, dpWay = 0;
if (n <= 1) return dp1;
if (n == 2) return dp2; while ((n--) - 2) {
dpWay = dp1 + dp2;
dp1 = dp2;
dp2 = dpWay;
}
return dpWay;
}
};

LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)

    题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...

  4. 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 ...

  5. [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 ...

  6. Climbing Stairs爬楼梯——动态规划

    题目描写叙述: 初阶:有n层的台阶,一開始你站在第0层,每次能够爬两层或者一层. 请问爬到第n层有多少种不同的方法? 进阶:假设每次能够爬两层.和倒退一层,同一个位置不能反复走,请问爬到第n层有多少种 ...

  7. 70. Climbing Stairs爬楼梯

    网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...

  8. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  9. LN : leetcode 70 Climbing Stairs

    lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...

  10. 42. leetcode 70. Climbing Stairs

    70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...

随机推荐

  1. Java中this与super

    l  对象的this引用 作用: this关键字就是让类中一个方法,访问该类中的另一个方法或属性. 1.构造器中引用该构造器正在初始化的对象. 2.在方法中引用调用该方法的对象(哪个对象调用的方法,t ...

  2. Install certificates needed for Visual Studio offline installation

    Visual Studio is primarily designed for installation from an internet-connected machine, since many ...

  3. 【Python】http.client库的用法

    代码: # http.client测试,该库较底层,不常用 import http.client conn=None try: conn=http.client.HTTPSConnection(&qu ...

  4. netlink error: too many arguments to function 'netlink_kernel_create'

    2.6版本的 netlink_kernel_create(&init_net, NETLINK_TEST, 0, NULL, kernel_receive ,THIS_MODULE);   3 ...

  5. Javascript的解析器

    Carakan C/C++ http://my.opera.com/core/blog/2009/02... SquirrelFish C++ http://trac.webkit.org/wiki/ ...

  6. ionic build android 失败 及 解决方案

    原因:没有接受以下SDK组件的许可协议 解决方案: install Android Support Repository

  7. 〖Linux〗Bash快捷键使用

    这篇 Bash Shell Shortcuts 的快捷键总结的非常好.值得学习.下面内容大多数是拷贝粘贴与总结. CTRL 键相关的快捷键: Ctrl + a - Jump to the start ...

  8. 14-spring学习-变量操作

    表达式所有操作都是可以以变量形式出现的. 观察变量的定义: package com.Spring.ELDemo; import org.springframework.expression.Evalu ...

  9. javascript 图片预加载

    <!DOCTYPE > <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta ...

  10. 一次安装win10 ubuntu16.0经过记录

    步骤摘要 三个U盘: 1.制作WIN8 PE启动盘,使用的软件为“U深度装机版”,可自行百度下载 2.制作WIN10系统安装盘,使用UltraISO,这里使用的win10镜像为: 链接: http:/ ...