70. 爬楼梯
题目描述
假设你正在爬楼梯。需要 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
方法一:递推
我们定义
初始条件为
答案即为
由于
时间复杂度
java
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;
}
}
cpp
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;
}
};
ts
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;
}
python
class Solution:
def climbStairs(self, n: int) -> int:
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return b
方法二:矩阵快速幂加速递推
我们设
我们希望根据
由于
第二列为:
因此有:
我们定义初始矩阵
时间复杂度
java
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;
}
}
cpp
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;
}
};
ts
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;
}
python
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]