题目描述

假设你正在爬楼梯。需要 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(爬楼梯)的更多相关文章

  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. Linux运维高级-核心知识提高

    一.Linux之定时任务crond 二.Linux之用户管理 三.Linux之初识磁盘 四.Linux之磁盘管理 五.Linux三剑客-SED 六.Linux三剑客-AWK 七.初识shell编程 八 ...

  2. InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'

    在新建asp.net core 应用后, 添加了自定义的ApplicationDbContext 和ApplicationUser ,并添加了Identity认证后, 会出现 InvalidOpera ...

  3. dotnetcore ueditor

    dotnetcore ueditor  https://github.com/durow/ueditornetcore http://www.cnblogs.com/durow/p/6116393.h ...

  4. A. Points in Segments(cf a题, 水题)

    没什么好说的 #include<iostream> using namespace std; ], x, y,n, m, ans; int main(){ cin>>n> ...

  5. 在Bootstrap开发框架的工作流模块中实现流程完成后更新资料状态处理

    在开发查看流程表单明细的时候,在Web界面中,我们往往通过使用@RenderPage实现页面内容模块化的隔离,减少复杂度,因此把一些常用的如审批.撤销.会签.阅办等等的流程步骤都放到了通用处理的页面V ...

  6. Surjectivity is stable under base change

    Nowadays, I close a new small case. Proposition. For a surjective morphism between scheme $X\stackre ...

  7. [转帖]Go中的下划线

    Go中的下划线 https://blog.csdn.net/wanglei9876/article/details/50475864 下划线的作用: 在import 时 是仅引入 init 函数 在正 ...

  8. 使用Node.js搭建数据爬虫crawler

    0. 通用爬虫框架包括: (1) 将爬取url加入队列,并获取指定url的前端资源(crawler爬虫框架主要使用Crawler类进行抓取网页) (2)解析前端资源,获取指定所需字段的值,即获取有价值 ...

  9. mysql client--笔记-修改密码-登录-查看数据库-创建数据库

    1 登录 mysql client 打开 mysql client -输入密码 123 回车 2 show database; ---显示数据库 3 切换数据库:use mysql 4 describ ...

  10. python 爬取可用

    #coding:utf-8 from bs4 import BeautifulSoup import time import threading import random import telnet ...