鱼C论坛

 找回密码
 立即注册
查看: 1343|回复: 0

[学习笔记] leetcode 70. Climbing Stairs

[复制链接]
发表于 2019-8-19 10:05:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
斐波那契数列的应用

  1. You are climbing a stair case. It takes n steps to reach to the top.

  2. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

  3. Note: Given n will be a positive integer.

  4. Example 1:

  5. Input: 2
  6. Output: 2
  7. Explanation: There are two ways to climb to the top.
  8. 1. 1 step + 1 step
  9. 2. 2 steps
  10. Example 2:

  11. Input: 3
  12. Output: 3
  13. Explanation: There are three ways to climb to the top.
  14. 1. 1 step + 1 step + 1 step
  15. 2. 1 step + 2 steps
  16. 3. 2 steps + 1 step
复制代码


  1. class Solution {
  2.     public int climbStairs(int n) {
  3.         
  4.         int[] array = new int[n+1];
  5.         
  6.         array[0] = 1;
  7.         array[1] = 1;
  8.         
  9.         for(int i = 2 ; i <= n ; i++){
  10.             
  11.             array[i] = array[i -1] + array[i -2];
  12.          
  13.         }
  14.         
  15.         return array[n];
  16.         
  17.     }
  18. }
复制代码



14.jpg

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 16:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表