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. 7z压缩与解压命令

    在写很多工具的时候,可能会用到7z命令来进行压缩与解压操作.这里记录二个比较常用的操作:压缩.解压.   在dos窗口下输入7z命令,会显示7z的使用参数详情: 7-Zip 9.10 beta  Co ...

  2. Android之循环显示图像的Android Gallery组件

    转自:http://www.blogjava.net/nokiaguy/archive/2010/08/23/329721.html Gallery组件主要用于横向显示图像列表,不过按常规做法.Gal ...

  3. 关于Spring-Data-Jpa的一些理解

    spring data jpa介绍 首先了解JPA是什么? JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管 ...

  4. Kubeadm安装Kubernetes环境

    Kubeadm方式号称一键安装部署,很多人也试过并且顺利成功,可到了我这里因为折腾系统问题,倒腾出不少的坑出来. kubeadm好处是自动配置了必要的服务,以及缺省配置了安全的认证,etcd,apis ...

  5. fabric 清理环境 运行SDK

    清理环境: rm -rf /tmp/* rm -rf ~/.hfc-key-store/ 启动网络: docker  ps -a create channel: join channel: 清理npm ...

  6. 7.3 netty3基本使用

    由于dubbo默认使用的是netty3进行通信的,这里简单的列出一个netty3通信的例子. 一 server端 1 Server package com.hulk.netty.server; imp ...

  7. Kudu-java数据库简单操作

    参考官网:http://kudu.apache.org/docs/kudu_impala_integration.html 参考:https://my.oschina.net/weiqingbin/b ...

  8. 大数据开发实战:Hive优化实战2-大表join小表优化

    4.大表join小表优化 和join相关的优化主要分为mapjoin可以解决的优化(即大表join小表)和mapjoin无法解决的优化(即大表join大表),前者相对容易解决,后者较难,比较麻烦. 首 ...

  9. Android实现录音的方法(最重要的是对MediaRecorder的试用方法)

    package cn.eoe.record; import java.io.File; import java.io.IOException; import android.app.Activity; ...

  10. java字符编码转换

    在开发的过程中.字符编码经常令我们头痛.经常会出现各种各样的乱码.以下就介绍java的编码转换和常见的乱码是使用什么样的编码去读取的: 先看一张图片: watermark/2/text/aHR0cDo ...