最长公共子序列
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
vector<vector
for(int i = 1; i <= text1.size(); ++i)
{
for(int j = 1; j <= text2.size(); ++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[text1.size()][text2.size()];
}
};
不相交的线
class Solution {
public:
int maxUncrossedLines(vector
vector<vector
for(int i = 1; i <= nums1.size(); ++i)
{
for(int j = 1; j <= nums2.size(); ++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[nums1.size()][nums2.size()];
}
};
最大子数组和
class Solution {
public:
int maxSubArray(vector
vector
dp[0] = nums[0];
int ret = 0;
int maxvalue = nums[0];
for(int i = 1; i < nums.size(); ++i)
{
dp[i] = max(dp[i-1] + nums[i], nums[i]);
if(dp[i] > maxvalue)
{
maxvalue = dp[i];
}
}
return maxvalue;
}
};
判断子序列
class Solution {
public:
bool isSubsequence(string s, string t) {
vector<vector
for(int i = 1; i <= s.size(); ++i)
{
for(int j = 1; j <= t.size(); ++j)
{
if(s[i-1] == t[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[s.size()][t.size()] == s.size();
}
};