NC261. 跳台阶扩展问题
描述
示例1
输入:
3
输出:
4
示例2
输入:
1
输出:
1
C++ 解法, 执行用时: 2ms, 内存消耗: 352KB, 提交时间: 2020-08-25
class Solution { public: int jumpFloorII(int number) { if(number<=0) return 0; if(number==1) return 1; if(number==2) return 2; vector<int> memo (number+1); int sum=1; for(int i=2;i<=number;++i){ memo[i]=sum+1; sum+=memo[i]; } return memo[number]; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2021-06-01
class Solution { public: int jumpFloorII(int number) { int* arr = new int[number+1]; arr[0] = 0; arr[1] = 1; for (int i = 2; i < number + 1;i++){ int t = 0; for (int j = 1; j < i; j++) t += arr[j]; arr[i]=t + 1; } int ret = arr[number]; delete[] arr; return ret; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2021-05-29
class Solution { public: int jumpFloorII(int number) { int *a = new int[number+1]; a[0] = 1; a[1] = 1; for(int i = 2;i <= number;i++){ a[i] = 0; for(int j = i-1;j >= 0; j--) a[i]+=a[j]; } return a[number]; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2021-05-26
class Solution { public: int jumpFloorII(int number) { vector<int>dp(number+1,0); dp[1]=1; dp[2]=2; int sum=3; for(int i=3;i<=number;i++){ dp[i]=sum+1; sum+=dp[i]; } return dp[number]; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2021-05-26
class Solution { public: int jumpFloorII(int number) { if(number==0||number==1) return 1; if(number==2) return 2; int sum=2; for(int i=3;i<=number;++i) { sum=2*sum; } return sum; } };