假设你正在爬楼梯。需要 n
阶你才能到达楼顶。
每次你可以爬 1
或 2
个台阶。你有多少种不同的方法可以爬到楼顶呢?
示例 1:
输入:n = 2 输出:2 解释:有两种方法可以爬到楼顶。 1. 1 阶 + 1 阶 2. 2 阶
示例 2:
输入:n = 3 输出:3 解释:有三种方法可以爬到楼顶。 1. 1 阶 + 1 阶 + 1 阶 2. 1 阶 + 2 阶 3. 2 阶 + 1 阶
提示:
1 <= n <= 45
我们定义
初始条件为
答案即为
由于
时间复杂度
class Solution:
def climbStairs(self, n: int) -> int:
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return b
class Solution {
public int climbStairs(int n) {
int a = 0, b = 1;
for (int i = 0; i < n; ++i) {
int c = a + b;
a = b;
b = c;
}
return b;
}
}
class Solution {
public:
int climbStairs(int n) {
int a = 0, b = 1;
for (int i = 0; i < n; ++i) {
int c = a + b;
a = b;
b = c;
}
return b;
}
};
func climbStairs(n int) int {
a, b := 0, 1
for i := 0; i < n; i++ {
a, b = b, a+b
}
return b
}
function climbStairs(n: number): number {
let p = 1;
let q = 1;
for (let i = 1; i < n; i++) {
[p, q] = [q, p + q];
}
return q;
}
impl Solution {
pub fn climb_stairs(n: i32) -> i32 {
let (mut p, mut q) = (1, 1);
for i in 1..n {
let t = p + q;
p = q;
q = t;
}
q
}
}
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function (n) {
let a = 0,
b = 1;
for (let i = 0; i < n; ++i) {
const c = a + b;
a = b;
b = c;
}
return b;
};
class Solution {
/**
* @param Integer $n
* @return Integer
*/
function climbStairs($n) {
if ($n <= 2) {
return $n;
}
$dp = [0, 1, 2];
for ($i = 3; $i <= $n; $i++) {
$dp[$i] = $dp[$i - 2] + $dp[$i - 1];
}
return $dp[$n];
}
}
我们设
我们希望根据 $Fib(n-1) = \begin{bmatrix} F_{n - 1} & F_{n - 2} \end{bmatrix}$ 推出
由于
第二列为:
因此有:
我们定义初始矩阵 $res = \begin{bmatrix} 1 & 1 \end{bmatrix}$,那么
时间复杂度
class Solution:
def climbStairs(self, n: int) -> int:
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
m, n = len(a), len(b[0])
c = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
for k in range(len(a[0])):
c[i][j] = c[i][j] + a[i][k] * b[k][j]
return c
def pow(a: List[List[int]], n: int) -> List[List[int]]:
res = [[1, 1]]
while n:
if n & 1:
res = mul(res, a)
n >>= 1
a = mul(a, a)
return res
a = [[1, 1], [1, 0]]
return pow(a, n - 1)[0][0]
class Solution {
private final int[][] a = {{1, 1}, {1, 0}};
public int climbStairs(int n) {
return pow(a, n - 1)[0][0];
}
private int[][] mul(int[][] a, int[][] b) {
int m = a.length, n = b[0].length;
int[][] c = new int[m][n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < a[0].length; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
private int[][] pow(int[][] a, int n) {
int[][] res = {{1, 1}, {0, 0}};
while (n > 0) {
if ((n & 1) == 1) {
res = mul(res, a);
}
n >>= 1;
a = mul(a, a);
}
return res;
}
}
class Solution {
public:
int climbStairs(int n) {
vector<vector<long long>> a = {{1, 1}, {1, 0}};
return pow(a, n - 1)[0][0];
}
private:
vector<vector<long long>> mul(vector<vector<long long>>& a, vector<vector<long long>>& b) {
int m = a.size(), n = b[0].size();
vector<vector<long long>> res(m, vector<long long>(n));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < a[0].size(); ++k) {
res[i][j] += a[i][k] * b[k][j];
}
}
}
return res;
}
vector<vector<long long>> pow(vector<vector<long long>>& a, int n) {
vector<vector<long long>> res = {{1, 1}, {0, 0}};
while (n) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
};
type matrix [2][2]int
func climbStairs(n int) int {
a := matrix{{1, 1}, {1, 0}}
return pow(a, n-1)[0][0]
}
func mul(a, b matrix) (c matrix) {
m, n := len(a), len(b[0])
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
for k := 0; k < len(a[0]); k++ {
c[i][j] += a[i][k] * b[k][j]
}
}
}
return
}
func pow(a matrix, n int) matrix {
res := matrix{{1, 1}, {0, 0}}
for n > 0 {
if n&1 == 1 {
res = mul(res, a)
}
a = mul(a, a)
n >>= 1
}
return res
}
function climbStairs(n: number): number {
const a = [
[1, 1],
[1, 0],
];
return pow(a, n - 1)[0][0];
}
function mul(a: number[][], b: number[][]): number[][] {
const [m, n] = [a.length, b[0].length];
const c = Array(m)
.fill(0)
.map(() => Array(n).fill(0));
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
for (let k = 0; k < a[0].length; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
function pow(a: number[][], n: number): number[][] {
let res = [
[1, 1],
[0, 0],
];
while (n) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function (n) {
const a = [
[1, 1],
[1, 0],
];
return pow(a, n - 1)[0][0];
};
function mul(a, b) {
const [m, n] = [a.length, b[0].length];
const c = Array(m)
.fill(0)
.map(() => Array(n).fill(0));
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
for (let k = 0; k < a[0].length; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
function pow(a, n) {
let res = [
[1, 1],
[0, 0],
];
while (n) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
import numpy as np
class Solution:
def climbStairs(self, n: int) -> int:
res = np.mat([(1, 1)], np.dtype("O"))
factor = np.mat([(1, 1), (1, 0)], np.dtype("O"))
n -= 1
while n:
if n & 1:
res *= factor
factor *= factor
n >>= 1
return res[0, 0]