代码随想录day44 1143.最长公共子序列 1035.不相交的线 53. 最大子序和 392.判断子序列
代码随想录
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int size1 = text1.size();
int size2 = text2.size();
vector<vector<int>> dp(size1 + 1, vector<int>(size2 + 1, 0));
for (int i = 1; i <= size1; i++) {
for (int j = 1; j <= size2; j++) {
if (text1[i - 1] == text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[size1][size2];
}
};
代码随想录
class Solution {
public:
int maxUncrossedLines(vector<int>& nums1, vector<int>& nums2) {
int size1 = nums1.size();
int size2 = nums2.size();
vector<vector<int>> dp(size1 + 1, vector<int>(size2 + 1, 0));
for (int i = 1; i <= size1; i++) {
for (int j = 1; j <= size2; j++) {
if (nums1[i - 1] == nums2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[size1][size2];
}
};
代码随想录
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int size = nums.size();
vector<int> dp(size);
dp[0] = nums[0];
int res = nums[0];
for (int i = 1; i < size; i++) {
dp[i] = max(dp[i - 1] + nums[i], nums[i]);
res = res < dp[i] ? dp[i] : res;
}
return res;
}
};
代码随想录
class Solution {
public:
bool isSubsequence(string s, string t) {
int size1 = s.size();
int size2 = t.size();
vector<vector<int>> dp(size1 + 1, vector<int>(size2 + 1, 0));
for (int i = 1; i <= size1; i++) {
for (int j = 1; j <= size2; j++) {
if (s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = dp[i][j - 1];
}
}
}
return dp[size1][size2] == size1;
}
};