子序列:
53. Maximum Subarray
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if(nums.size() == 1) return nums[0];
vector<int> dp(nums.size(), 0);
dp[0] = nums[0];
int res = nums[0];
for(int i=1; i<nums.size(); i++){
dp[i] = max(dp[i-1]+nums[i], nums[i]);
res = max(res, dp[i]);
}
return res;
}
};
392. Is Subsequence
1.双指针
class Solution {
public:
bool isSubsequence(string s, string t) {
int i=0, j=0;
while(i<s.size() && j<t.size()){
if(s[i] == t[j]){
i++;
}
j++;
}
return i==s.size();
}
};
2.动态规划
class Solution {
public:
bool isSubsequence(string s, string t) {
if(s.size() > t.size()) return false;
vector<vector<int>> dp(s.size()+1, vector<int> (t.size()+1, 0));
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] = dp[i][j-1];
}
}
}
if(dp[s.size()][t.size()] == s.size()) return true;
return false;
}
};
115. Distinct Subsequences
class Solution {
public:
int numDistinct(string s, string t) {
vector<vector<uint64_t>> dp(s.size()+1, vector<uint64_t> (t.size()+1, 0));
for(int i=0; i<=s.size(); i++){
dp[i][0] = 1;
}
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]+dp[i-1][j];
}else{
dp[i][j] = dp[i-1][j];
}
}
}
return dp[s.size()][t.size()];
}
};
初始化:
这里感觉对我来说有点难以理解
dp[i][0] 这里我的理解是,以下标i-1位结尾的s和空集能够相同的子序列的个数,那么肯定只有一个,就是把s里的全部删掉
dp[0][j]s是空数组所以不可能和t一样(除了t是空数组的时候,所以dp[0][0]为1)
注意:
这里不能直接用int会报错文章来源:https://www.toymoban.com/news/detail-648017.html
583. Delete Operation for Two Strings
class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int>> dp(word1.size()+1, vector<int> (word2.size()+1, 0));
for(int i=0; i<=word1.size(); i++) dp[i][0] = i;
for(int j=0; j<=word2.size(); j++) dp[0][j] = j;
for(int i=1; i<=word1.size(); i++){
for(int j=1; j<=word2.size(); j++){
if(word1[i-1] == word2[j-1]){
dp[i][j] = dp[i-1][j-1];
}else{
dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1);
}
}
}
return dp[word1.size()][word2.size()];
}
};
二叉树:
102. Binary Tree Level Order Traversal
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if(root != NULL) que.push(root);
vector<vector<int>> res;
while(!que.empty()){
int size = que.size();
vector<int> vec;
for(int i=0; i<size; i++){
TreeNode* cur = que.front();
vec.push_back(cur->val);
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
res.push_back(vec);
}
return res;
}
};
层序遍历文章来源地址https://www.toymoban.com/news/detail-648017.html
107. Binary Tree Level Order Traversal II
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
queue<TreeNode*> que;
if(root != NULL) que.push(root);
vector<vector<int>> res;
while(!que.empty()){
int size = que.size();
vector<int> vec;
for(int i=0; i<size; i++){
TreeNode* cur = que.front();
que.pop();
vec.push_back(cur->val);
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
res.push_back(vec);
}
reverse(res.begin(), res.end());
return res;
}
};
到了这里,关于Leetcode 子序列 53 392 115 583的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!