代码随想录day55

392判断子序列

class Solution {
public:
    bool isSubsequence(string s, string t) 
    {
        vector>dp(s.size()+1,vector(t.size()+1,0));
        int res=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];
                res=res>dp[i][j]?res:dp[i][j];
            }
        }
        if(res == s.size()) return true;
        return false;
    }
};

792匹配子序列的单词书

class Solution {
public:
    int numMatchingSubseq(string s, vector& words) 
    {
        vector> index(256);
        for(int i=0;i

115不同的子序列

class Solution {
public:
    int numDistinct(string s, string t) {
        vector> dp(s.size() + 1, vector(t.size() + 1));
        for (int i = 0; i < s.size(); i++) dp[i][0] = 1;
        for (int j = 1; j < t.size(); j++) dp[0][j] = 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] + dp[i - 1][j];
                } 
                else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        return dp[s.size()][t.size()];
    }
};

你可能感兴趣的:(算法,leetcode,数据结构,动态规划)