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?

方法一:递归

public int climbStairs2(int n) {if (n == 0)return 1;if (n < 3)return n;return climbStairs(n - 1) + climbStairs(n - 2);}

方法二:用临时变量保存前面两个数

public int climbStairs(int n) {if (n <= 0)return n == 0 ? 1 : 0;int result = 0;if (n < 3)return n;int pre = 1;int last = 2;for (int i = 3; i <= n; i++) {result = last + pre;pre = last;last = result;}return result;}

总结:方法一之所以能够优化为方法二是因为方法一计算了f(n-1)和f(n-2)(注意:这里f(n)指的是爬上第n阶的方法数),因此我们用两个临时变量保存就好了

与这题类似的另外一个题是:

There is a fence with n posts, each post can be painted with one of thek colors.
You have to paint all the posts such that no more than two adjacent fence posts have the same color.
Return the total number of ways you can paint the fence.

该题也是类似的原理

public class Solution {/*** @param n non-negative integer, n posts* @param k non-negative integer, k colors* @return an integer, the total number of ways*/public int numWays(int n, int k) {if (n == 0 || k == 0)return 0;if (n < 3) {return n == 1 ? k : k * k;}int pre = k;int last = k * k;int result = 0;for (int i = 3; i <= n; n++) {result = (k - 1) * pre + (k - 1) * last;pre = last;last = result;}return result;}
}

转载于:https://www.cnblogs.com/googlemeoften/p/5833911.html

LeetCode Climbing Stairs相关推荐

  1. LeetCode 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 | Climbing Stairs

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

  3. 【动态规划 斐波那切数列】LeetCode 746. Min Cost Climbing Stairs

    LeetCode 746. Min Cost Climbing Stairs 本博客转载自:http://www.cnblogs.com/grandyang/p/8343874.html 存在无代价的 ...

  4. 【斐波那切数列】LeetCode 70. Climbing Stairs

    LeetCode 70. Climbing Stairs 这是一道利用斐波那切数列求解的题目.求斐波那切数列有比较经典的4种方法 (1)递归法:复杂度太高 (2)迭代法:时间复杂度为O(n)O(n)O ...

  5. [勇者闯LeetCode] 70. Climbing Stairs

    [勇者闯LeetCode] 70. Climbing Stairs Description You are climbing a stair case. It takes n steps to rea ...

  6. [LeetCode]70.Climbing Stairs

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

  7. 算法:Climbing Stairs(爬楼梯) 6种解法

    说明 算法:Climbing Stairs(爬楼梯) LeetCode地址:https://leetcode.com/problems/climbing-stairs/ 题目: You are cli ...

  8. 10.2 动态规划算法套路及空间优化 —— Climbing Stairs Unique Paths

    这一篇文章从最简单的动态规划题目开始,结合上一节动态规划三要素,以LeetCode两道基础的DP题目阐述DP问题的基本套路解法. 70. Climbing Stairs You are climbin ...

  9. 70. Climbing Stairs

    70. Climbing Stairs 1. 题目 You are climbing a stair case. It takes n steps to reach to the top. Each ...

最新文章

  1. 构建之法阅读笔记06
  2. 小白学python买什么书-小白如何高效率学习python?真心建议(附教程)
  3. mysql5.7导入数据的权限问题
  4. 【LeetCode从零单排】No221.Maximal Square
  5. graphpad的折线图x轴自定义_Graphpad Prism绘制折线图
  6. 2021ICPC(沈阳) - String Problem(后缀树+贪心)
  7. System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误
  8. 你爱我吗? | 今日最佳
  9. 阿里文娱实战 | 小而美的 egg-react-ssr 开源实现方案
  10. kubernetest pod为ContainerCreating、ImagePullBackOff状态 怎么办
  11. JavaCV 绘制多边形
  12. Unity - Projector - 实时[假]阴影
  13. SDOI 2014 数表 题解
  14. 淘宝最基础的优化:标题优化
  15. xp系统网上邻居看不到局域网电脑_Win7系统网上邻居看不到局域网中其他电脑怎么办...
  16. resolution will not be reattempted until the update interval of XXX has elapsed or updates are force
  17. 回顾《网易数据基础平台建设》
  18. 【时间序列分析】差分运算及延迟算子的性质
  19. firefox打开不能上网怎么回事 firefox 不能上网
  20. 【二】2D测量 Metrology——read_metrology_model()算子

热门文章

  1. python-常见数据类型及其方法
  2. 浅谈promise用es5实现
  3. 前段框架——VueX
  4. 零基础学习JavaSE(一)
  5. android,项目,一些教程
  6. BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告
  7. 简单实用的Windows命令(一)
  8. QQ截屏 抓级联菜单、下拉菜单及右键弹出菜单图
  9. 从零开始实现一个简易的Java MVC框架(六)--加强AOP功能
  10. [MetalKit]2-Using-MetalKit-part-1使用MetalKit1