Leetcode#70. Climbing Stairs(爬楼梯)
题目描述
假设你正在爬楼梯。需要 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(爬楼梯)的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)
题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...
- 70. Climbing Stairs爬楼梯
网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...
- 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 ...
- 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 ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
随机推荐
- springboot操作mongodb
springboot操作mongodb 采用MongoRepository操作mongodb springboot版本2.1.2.RELEASE 注意的是:在运行应用程序时,会报错OSS Algori ...
- Spring Security(三十一):9.6 Localization(本地化)
Spring Security supports localization of exception messages that end users are likely to see. If you ...
- Scrapy安装报错
python3 pip 安装Scrapy在win10 安装报错error: Microsoft Visual C++ 14.0 is required. Get it with "Micro ...
- Jquery mobile动态生成ListView调用刷新方法报错
错误:cannot call methods on listview prior to initialization... 示例: <div data-role="page" ...
- 周末学习笔记——day01(函数,函数对象,嵌套调用,名称空间,作用域,闭包,装饰器)
一,复习 字符编码 文件头:py2—ASCII,py3—UTF-8 三种字符串:u' ' b ' ' r ' ' u' ' .endcode(' utf-8 ') b' '.deconde(' utf ...
- ondaHTTPError: HTTP 000 CONNECTION FAILED for url
可能是网络问题,换网络. 可能是获取库的IP无法链接到,换库的IP,如添加清华镜像IP等.
- RabbitMQ学习笔记一:本地Windows环境安装RabbitMQ Server
一:安装RabbitMQ需要先安装Erlang语言开发包,百度网盘地址:http://pan.baidu.com/s/1jH8S2u6.直接下载地址:http://erlang.org/downloa ...
- 国内可访问的稳定docker镜像
可参考:https://yeasy.gitbooks.io/docker_practice/content/install/mirror.html 但在debian 9上进行相应配置后,在pull镜像 ...
- 关于PHP自动捕捉处理错误和异常的尝试
之所以想着做错误和异常的自动处理是因为: 用的公司自己的框架写API,没有异常和错误相关功能, 而每次操作都进行try...catch,有点繁琐不说,感觉还很鸡肋,即使我catch到了,还是得写代码进 ...
- vue非父子组件之间的通信
https://www.cnblogs.com/chengduxiaoc/p/7099552.html //vm.$emit( event, arg ) //触发当前实例上的事件 //vm.$on ...