标签:数字 公式 代码 amp display 小结 fun 解释 http
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
注意:给定 n 是一个正整数。
示例 1:
输入: 2
输出: 2
解释: 有两种方法可以爬到楼顶。
- 1 阶 + 1 阶
- 2 阶
示例 2:
输入: 3
输出: 3
解释: 有三种方法可以爬到楼顶。
- 1 阶 + 1 阶 + 1 阶
- 1 阶 + 2 阶
- 2 阶 + 1 阶
代码:
class Solution {
public int climbStairs(int n) {
if (n <= 2) {
return n;
}
return climbStairs(n - 1) + climbStairs(n - 2);
}
}
代码:
class Solution {
Map<Integer, Integer> map = new HashMap<>();
public int climbStairs(int n) {
if (n <= 2) {
return n;
}
if (map.containsKey(n)) {
return map.get(n);
}
int result = climbStairs(n - 1) + climbStairs(n - 2);
map.put(n, result);
return result;
}
}
代码:
class Solution {
public int climbStairs(int n) {
if (n <= 2) {
return n;
}
int[] dp = new int[n];
dp[0] = 1;
dp[1] = 2;
for (int i = 2; i < n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n - 1];
}
}
代码:
class Solution {
public int climbStairs(int n) {
if (n <= 2) {
return n;
}
int pre = 1, cur = 2;
for (int i = 2; i < n; i++) {
int next = pre + cur;
pre = cur;
cur = next;
}
return cur;
}
}
\[ \begin{matrix} \left[ \begin{matrix} 1 & 0\ 0 & 0 \end{matrix} \right] \left[ \begin{matrix} 1 & 1\ 1 & 0 \end{matrix} \right] = \left[ \begin{matrix} 1+0 & 1+0\ 0+0 & 0+0 \end{matrix} \right] = \left[ \begin{matrix} 1 & 1\ 0 & 0 \end{matrix} \right] \end{matrix} \]
\[ \begin{matrix} \left[ \begin{matrix} 1 & 1\ 0 & 0 \end{matrix} \right] \left[ \begin{matrix} 1 & 1\ 1 & 0 \end{matrix} \right] = \left[ \begin{matrix} 1+1 & 1+0\ 0+0 & 0+0 \end{matrix} \right] = \left[ \begin{matrix} 2 & 1\ 0 & 0 \end{matrix} \right] \end{matrix} \]
\[ \begin{matrix} \left[ \begin{matrix} F_n & F_{n-1}\ 0 & 0 \end{matrix} \right] \left[ \begin{matrix} 1 & 1\ 1 & 0 \end{matrix} \right] = \left[ \begin{matrix} F_n+F_{n-1} & F_n\ 0 & 0 \end{matrix} \right] = \left[ \begin{matrix} F_{n+1} & F_n\ 0 & 0 \end{matrix} \right] \end{matrix} \]
代码:
class Solution {
public int climbStairs(int n) {
if (n <= 2) {
return n;
}
int[][] matrix = { { 2, 1 }, { 0, 0 } };
int[][] func = { { 1, 1 }, { 1, 0 } };
for (int i = 2; i < n; i++) {
matrix = multiply(matrix, func);
}
return matrix[0][0];
}
private int[][] multiply(int[][] a, int[][] b) {
int[][] c = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j];
}
}
return c;
}
}
在上一个方法中,每次都是乘一个相同的矩阵;而同一数字多个相乘即幂运算,可以用二分法优化成快速幂,而矩阵也同样可以使用,先计算\(M^{n/2}\),然后在用矩阵相乘的公式即可。
矩阵的起始乘积不再是 1,而是单位矩阵\(\left[ \begin{matrix} 1 & 0\\ 0 & 1 \end{matrix} \right]\)。
在这题我们就要把矩阵初始为\(\left[ \begin{matrix} 1 & 1\\ 1 & 0 \end{matrix} \right]\),矩阵结构改为\(\left[ \begin{matrix} F_n & F_{n-1}\\ F_{n-1} & F_{n-2} \end{matrix} \right]\)。
代码:
class Solution {
public int climbStairs(int n) {
if (n <= 2) {
return n;
}
int[][] matrix = { { 1, 1 }, { 1, 0 } };
int[][] result = pow(matrix, n);
return result[0][0];
}
private int[][] pow(int[][] matrix, int n) {
int[][] result = { { 1, 0 }, { 0, 1 } };
for (int i = n; i > 0; i /= 2) {
if ((i & 1) != 0) {
result = multiply(matrix, result);
}
matrix = multiply(matrix, matrix);
}
return result;
}
private int[][] multiply(int[][] a, int[][] b) {
int[][] c = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j];
}
}
return c;
}
}
\[ F(n) = \cfrac{((\cfrac{1+\sqrt[2]{5}}{2})^n-(\cfrac{1-\sqrt[2]{5}}{2})^n)}{\sqrt[2]{5}} \]
因为是使用原斐波那契数列的第 2 项和第 3 项作为初始项,所以 n 需要 加 1。
代码:
class Solution {
public int climbStairs(int n) {
double sqrt5 = Math.sqrt(5);
return (int) ((Math.pow((1 + sqrt5) / 2, n + 1) - Math.pow((1 - sqrt5) / 2, n + 1)) / sqrt5);
}
}
这题和 509. 斐波那契数 是同一问题,只是初始项不同,这题的初始项是 1 和 2,且 n 不可取 0。
除了初始处理和 n 的边界处理有点不一样,方法还是一样的。
标签:数字 公式 代码 amp display 小结 fun 解释 http
原文地址:https://www.cnblogs.com/qiu_jiaqi/p/LeetCode_70.html