题目描述

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

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

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

示例 1:

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

示例 2:

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

思路

思路一:

递归

思路二:

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

代码实现

  1. package DynamicProgramming;
  2. /**
  3. * 70. Climbing Stairs(爬楼梯)
  4. * 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
  5. * 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
  6. */
  7. public class Solution70 {
  8. public static void main(String[] args) {
  9. Solution70 solution70 = new Solution70();
  10. System.out.println(solution70.climbStairs(3));
  11. }
  12. /**
  13. * 直接用递归
  14. *
  15. * @param n
  16. * @return
  17. */
  18. public int climbStairs(int n) {
  19. if (n <= 2) {
  20. return n;
  21. } else {
  22. return climbStairs(n - 1) + climbStairs(n - 2);
  23. }
  24. }
  25. /**
  26. * 用迭代的方法,用两个变量记录f(n-1) f(n-2)
  27. *
  28. * @param n
  29. * @return
  30. */
  31. public int climbStairs_2(int n) {
  32. int one = 1, two = 2, fN = 0;
  33. if (n <= 0) {
  34. return 0;
  35. } else if (n <= 2) {
  36. return n;
  37. } else {
  38. for (int i = 3; i <= n; i++) {
  39. fN = one + two;
  40. one = two;
  41. two = fN;
  42. }
  43. return fN;
  44. }
  45. }
  46. }

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前端导出Excel表格后数字自动变成科学计数法问题

    一般的文件导出都是后端进行导出,最近一个项目遇到导出接口挂掉了,前端实现导出的情况. 背景是vue框架,iView组件.可以直接使用exportCsv方法进行导出. 导出时进行一下行和列的切割就可以了 ...

  2. Python编码规范(PEP8)及奇技淫巧(不断更新)

    https://blog.csdn.net/MrLevo520/article/details/69155636

  3. python之迭代器、生成器、面向过程编程

    一 迭代器 一 迭代的概念 #迭代器即迭代的工具,那什么是迭代呢?#迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 while True: #只是单纯地重复,因而不 ...

  4. 【转】localStorage使用总结

    原文地址:https://www.cnblogs.com/st-leslie/p/5617130.html 一.什么是localStorage.sessionStorage 在HTML5中,新加入了一 ...

  5. 30分钟了解Springboot整合Shiro

    项目结构截图: 项目在结构上没有任何特殊之处,基本就是MVC的传统结构重点需要关注的是3个Entity类.2个Controller类和1个Config类. 首先,提供pom的完整文档结构: <p ...

  6. Linux之文本编辑器Vim

    一.什么是vim vi是一种模式编辑器.vi 是Unix世界里极为普遍的全屏幕文本编辑器,几乎可以说任何一台Unix机器都会提供这套软体,其他的文本编辑器则不一定会存在,但是目前我们使用比较多的是 v ...

  7. 使用bat脚本永久激活Windows系统(摘抄)

    使用bat脚本永久激活Windows系统   每次重装完系统后,右下角会提示系统未激活,无法进行一些个性化设置. 在这里我自己写了一个bat脚本用于激活Windows系统.(仅供学习) 文件下载: 链 ...

  8. python doc格式转文本格式

    首先python是不能直接读写doc格式的文件的,这是python先天的缺陷.但是可以利用python-docx (0.8.6)库可以读取.docx文件或.txt文件,且一路畅通无阻. 这样的话,可以 ...

  9. SpringCloud 过滤器

    在网关中配置过滤器 验证签名 package com.kps.zuul.filter; import com.kps.common.BodyReaderHttpServletRequestWrappe ...

  10. 【并发编程】【JDK源码】J.U.C--线程池

    原文:慕课网实战·高并发探索(十四):线程池 Executor new Thread的弊端 每次new Thread 新建对象,性能差. 线程缺乏统一管理,可能无限制的新建线程,相互竞争,可能占用过多 ...