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?

现在说一下大致思路:求出递推公式

f(n)=f(n-1)+f(n-2) ===>f(n)+f(n-1)=2f(n-1)+f(n-2)

[f(n) f(n-1)]=[[1 1][1 0]][f(n-1) f(n-2)]

可以得到递推矩阵

所以该算法的关键点就是:1.需要求出递推矩阵;2.写一个方法,能够实现矩阵相乘

虽然代码量会比其他几个方法大,但是算法复杂度比较低

* 动态规划解法
*/
public int climbStairs3(int n) {
if (n <= )
return ;
if (n == )
return ;
if (n == )
return ; int[][] base = { { , }, { , } };
int[][] res = matrixPower(base, n - ); return *res[][] + res[][];
}
/*
* 两个矩阵相乘
*/
private int[][] muliMatrix(int[][] m1, int[][] m2) {
int[][] res = new int[m1.length][m2[].length];
for (int i = ; i < m1.length; i++) {
for (int j = ; j < m2[].length; j++) {
for (int k = ; k < m2.length; k++) {
res[i][j] += m1[i][k] * m2[k][j];
}
}
}
return res;
}

包含三种最常用的回答,第一最优,第三最差

* 空间复杂度O();
*/
public int climbStairs(int n) {
if (n < )
return n;
int one_step_before = ;
int two_steps_before = ;
int all_ways = ;
for (int i = ; i < n; i++) {
all_ways = one_step_before + two_steps_before;
two_steps_before = one_step_before;
one_step_before = all_ways;
}
return all_ways;
}
/*
* 空间复杂度O(n);
*/
public int climbStairs_2(int n) {
if (n < )
return n;
int[] res = new int[n + ];
res[] = ;
res[] = ;
for (int i = ; i <= n; i++) {
res[i] = res[i - ] + res[i - ];
}
return res[n];
}
/*
* 方法一:递归 时间复杂度高
*/
public int climbStairs_1(int n) {
if (n < )
return ;
if (n == )
return ;
if (n == )
return ;
return climbStairs_1(n - ) + climbStairs_1(n - );
}

斐波那契数列

class Solution {
public:
int climbStairs(int n) {
int f = ;
int g = ;
while(n--){
f += g;
g = f -g;
}
return f;
}
};

climbing-stairs-动态规划,爬楼梯的路径数的更多相关文章

  1. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  2. 力扣—climbing stairs(爬楼梯) python实现

    题目描述: 中文: 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 英文: You are cl ...

  3. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

  4. LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)

    翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...

  5. LeetCode746 Min Cost Climbing Stairs(爬上楼梯的最小损失)

    题目 On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you p ...

  6. 动态规划-爬楼梯问题java实现

    最近开始看算法导论,研究了一下动态规划,下面就开始直入主题开始记录近期看的第一个知识点动态规划.提起动态规划就不得不提几个动态规划的金典问题爬楼梯.国王金矿.背包问题.今天就仔细分析一下爬楼梯问题. ...

  7. 70. Climbing Stairs(动态规划)

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

  8. 【easy】746. Min Cost Climbing Stairs 动态规划

    On a staircase, the i-th step has some non-negative cost cost[i]assigned (0 indexed). Once you pay t ...

  9. 746. Min Cost Climbing Stairs(动态规划)

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

随机推荐

  1. 阿里云linux图形界面(centos6)

    阿里云linux图形界面的安装方法:安装gnome图形化桌面#yum groupinstall -y "X Window System"#yum groupinstall -y & ...

  2. 用Eclipse给安卓应用进行签名

    Eclipse功能强大,用它来给应用进行签名也十分简单.下面是进行签名的步骤

  3. Caffe SSD的resize过程解析

    问题描述在windows平台上,本地训练SSD_512得到了对应的权值参数文件,加载模型进行前向测试的时候,发现调用caffe.io.Transformer中的resize处理函数速度太慢,打算用op ...

  4. PHP 5.4.17 发布!

    PHP 5.4.17发布.2013-07-04 经过1个RC 上个版本是2013-06-07的5.4.16.修正了大约20个Bug以及几个安全漏洞.尽管5.5.0正式版已经发布.但5.4还未停止更新. ...

  5. 请求被中止: 未能创建 SSL/TLS 安全通道,设置 TLSv1.2和TLSv1.1版本 .基础链接已经关闭,发送时发生错误

    WSO2 API访问的安全要求, 只能提供TLSv1.2和TLSv1.1版本,其它SSL版本协议因为存在较高安全漏洞问题会被disable. A 改成TLSv1.1或TLSv1.2,最好使用TLSv1 ...

  6. go语言之进阶篇 channel介绍

    1.channel介绍 和map类似,channel也一个对应make创建的底层数据结构的引用. 当我们复制一个channel或用于函数参数传递时,我们只是拷贝了一个channel引用,因此调用者何被 ...

  7. go语言之进阶篇error接口应用

    1.error接口应用 示例: package main import "fmt" import "errors" func MyDiv(a, b int) ( ...

  8. C#中转义字符[转]

    https://www.cnblogs.com/muran/p/3174865.html 编程中很多细节问题我们都要十分的注意,要不一个小小的字母错误就能引起程序的无法运行. C#中转义字符分2中,一 ...

  9. [leetcode]Remove Duplicates from Sorted List II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...

  10. Add Binary Leetcode java

    题目: Given two binary strings, return their sum (also a binary string). For example, a = "11&quo ...