示例代码

class Solution {
    // pow(10, 9) 返回的是 double 类型,而你希望 mod 是一个整数类型。
    //  static const int mod = pow(10,9)+7;
    int mod = 1000000000 + 7;
public:
    int numDistinct(string s, string t) {
        int len1=s.length(),len2=t.length();
        vector<vector<int>> dp(len1+1,vector<int>(len2+1));
        for(int i=0;i<=len1;i++){
            dp[i][0]=1;
        }
        for(int i=1;i<=len1;i++){
            for(int j=1;j<=len2;j++){
                if(s[i-1]==t[j-1]){
                    dp[i][j]=(dp[i-1][j-1]+dp[i-1][j])%(mod);
                }else{
                    dp[i][j]=dp[i-1][j];
                }
            }
        }
        return dp[len1][len2];
    }
};
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

效果展示

LeetCode---115. 不同的子序列(动态规划)_示例代码