提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
一、343整数拆分
cpp
class Solution {
public:
int integerBreak(int n) {
vector<int> dp(n + 1, 1);
for (int i = 2; i < n + 1; i ++) {
for (int j = 1; j <= i / 2; j ++) {
int num1 = max(j, dp[j]);
int num2 = max(i - j, dp[i-j]);
dp[i] = max(num1 * num2, dp[i]);
}
}
return dp[n];
}
};
二、96不同的二叉搜索树
cpp
class Solution {
public:
int numTrees(int n) {
vector<int> dp(n + 1, 1);
for (int i = 2; i <= n; i ++) {
//j为根结点的值
int count = 0;
for (int j = i; j > 0; j --) {
int left = j - 1;
int right = i - j;
count += dp[left] * dp[right];
}
dp[i] = count;
}
return dp[n];
}
};