C语言中并没有提供阶乘函数,自己实现很简单,阶乘英文为multiplyNumbers.
原本打算用递归阶乘暴力解决,虽然在本地VScode可以ac,但是在线编译器无法通过,显示
runtime error: signed integer overflow: 2004189184 * 6 cannot be represented in type 'int' (solution.c)
显然是阶乘结果太长,int类型无法接受这么长的数据。考虑其他思路。
-
动态规划
-
Fibonacci第n项数
class Solution {
public int climbStairs(int n) {
if (n <= 1) return 1;
else if (n == 2) return 2;
else {
int res = 0;
int i = 1, j = 2;
int k = 3;
while (k <= n) {
res = i + j;
i = j;
j = res;
k++;
}
return res;
}
}
}
- 记忆化搜索