LeetCode Climbing Stairs
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相关推荐
- LeetCode Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- LeetCode | Climbing Stairs
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- 【动态规划 斐波那切数列】LeetCode 746. Min Cost Climbing Stairs
LeetCode 746. Min Cost Climbing Stairs 本博客转载自:http://www.cnblogs.com/grandyang/p/8343874.html 存在无代价的 ...
- 【斐波那切数列】LeetCode 70. Climbing Stairs
LeetCode 70. Climbing Stairs 这是一道利用斐波那切数列求解的题目.求斐波那切数列有比较经典的4种方法 (1)递归法:复杂度太高 (2)迭代法:时间复杂度为O(n)O(n)O ...
- [勇者闯LeetCode] 70. Climbing Stairs
[勇者闯LeetCode] 70. Climbing Stairs Description You are climbing a stair case. It takes n steps to rea ...
- [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 ...
- 算法:Climbing Stairs(爬楼梯) 6种解法
说明 算法:Climbing Stairs(爬楼梯) LeetCode地址:https://leetcode.com/problems/climbing-stairs/ 题目: You are cli ...
- 10.2 动态规划算法套路及空间优化 —— Climbing Stairs Unique Paths
这一篇文章从最简单的动态规划题目开始,结合上一节动态规划三要素,以LeetCode两道基础的DP题目阐述DP问题的基本套路解法. 70. Climbing Stairs You are climbin ...
- 70. Climbing Stairs
70. Climbing Stairs 1. 题目 You are climbing a stair case. It takes n steps to reach to the top. Each ...
最新文章
- 构建之法阅读笔记06
- 小白学python买什么书-小白如何高效率学习python?真心建议(附教程)
- mysql5.7导入数据的权限问题
- 【LeetCode从零单排】No221.Maximal Square
- graphpad的折线图x轴自定义_Graphpad Prism绘制折线图
- 2021ICPC(沈阳) - String Problem(后缀树+贪心)
- System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误
- 你爱我吗? | 今日最佳
- 阿里文娱实战 | 小而美的 egg-react-ssr 开源实现方案
- kubernetest pod为ContainerCreating、ImagePullBackOff状态 怎么办
- JavaCV 绘制多边形
- Unity - Projector - 实时[假]阴影
- SDOI 2014 数表 题解
- 淘宝最基础的优化:标题优化
- xp系统网上邻居看不到局域网电脑_Win7系统网上邻居看不到局域网中其他电脑怎么办...
- resolution will not be reattempted until the update interval of XXX has elapsed or updates are force
- 回顾《网易数据基础平台建设》
- 【时间序列分析】差分运算及延迟算子的性质
- firefox打开不能上网怎么回事 firefox 不能上网
- 【二】2D测量 Metrology——read_metrology_model()算子