题目描述

假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

注意:给定 n 是一个正整数。

示例 1:

输入: 2
输出: 2
解释: 有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶

示例 2:

输入: 3
输出: 3
解释: 有三种方法可以爬到楼顶。
1. 1 阶 + 1 阶 + 1 阶
2. 1 阶 + 2 阶
3. 2 阶 + 1 阶

思路

思路一:

递归

思路二:

用迭代的方法,用两个变量记录f(n-1) f(n-2)

代码实现

package DynamicProgramming;

/**
* 70. Climbing Stairs(爬楼梯)
* 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
* 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
*/
public class Solution70 {
public static void main(String[] args) {
Solution70 solution70 = new Solution70();
System.out.println(solution70.climbStairs(3));
} /**
* 直接用递归
*
* @param n
* @return
*/
public int climbStairs(int n) {
if (n <= 2) {
return n;
} else {
return climbStairs(n - 1) + climbStairs(n - 2);
}
} /**
* 用迭代的方法,用两个变量记录f(n-1) f(n-2)
*
* @param n
* @return
*/
public int climbStairs_2(int n) {
int one = 1, two = 2, fN = 0;
if (n <= 0) {
return 0;
} else if (n <= 2) {
return n;
} else {
for (int i = 3; i <= n; i++) {
fN = one + two;
one = two;
two = fN;
}
return fN;
}
}
}

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爬楼梯 (C++)

    题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

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

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

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

  6. 70. Climbing Stairs爬楼梯

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

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

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

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

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

随机推荐

  1. js 获取纯web地址栏中URL传参

       function GetQueryString(name)    {         var reg = new RegExp("(^|&)"+ name +&quo ...

  2. Linux后台命令的使用说明

    1)ctrl+Z:停止当前进程 首先先将一个程序运行起来,这个时候如果你需要去干别的事情,需要暂停运行,可以使用ctrl+Z: user@mine:/opt/user/pytorch-gender$ ...

  3. 使用Fabric Node SDK进行Invoke和Query

    前面的文章都是在讲解Fabric网络的搭建和ChainCode的开发,那么在ChainCode开发完毕后,我们就需要使用Fabric SDK做应用程序的开发了.官方虽然提供了Node.JS,Java, ...

  4. 使用jprofiler分析dump文件一个实例

    3 .dump 线上文件栈 [root@yszyz10a153 ~]# jmap -dump:live,format=b,file=heap201712.hropf  72947 参考:https:/ ...

  5. HNOI2019做题笔记

    代码比较长所以直接去LOJ看吧- 鱼(计算几何.向量) 比较套路的内容:枚举\(D\),对于其他所有点按照\(D\)极角排序,按照极角序枚举\(A\),这样垂直于\(AD\)的线也会以极角序旋转,可以 ...

  6. odoo11 访问MSQL Server等第三发数据源

    odoo框架默认的访问时Postgres数据库,但在实际的应用场景中,不可避免的使用到其他数据库,所以有必要研究如何连接其他第三方数据库,这里分享下OCA的相关模块,具体的源代码在这里. 我将第三方的 ...

  7. 家庭记账本小程序之框架设计(java web基础版一)

    1.设计主页 main.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&quo ...

  8. Docker Toolbox替换默认docker machine的存储位置

    使用Docker Toolbox是因为它不用打开windows的hyper-v组件,这样可以和VMware workstation一起使用. 关于如何迁移可参考:https://www.cnblogs ...

  9. tomcat 启动窗口乱码

    在tomcat主目录下的conf文件夹里,找到logging.properties文件: 用记事本打开,找到以下内容 java.util.logging.ConsoleHandler.encoding ...

  10. ReentrantLock重入锁详解

    1.定义 重入锁:能够支持一个线程对资源的重复加锁,也就是当一个线程获取到锁后,再次获取该锁时而不会被阻塞. 2.可重入锁的应用场景 2.1 如果已经加锁,则不再重复加锁,比如:交互界面点击后响应时间 ...