参考https://oj.leetcode.com/problems/distinct-subsequences

动态规划方程

dp[i][j]=dp[i-1][j-1]+dp[i-1][j] (s(i)==t(i))

dp[i][j]=dp[i-1][j];

边界条件:  iif(j==0) d[i][j]=1;

自己画个矩阵看看。

可能出错,

1.直接递归超时

 public class Solution {
public int numDistinct(String S, String T) {
int len1=S.length();
int len2=T.length();
if(len1<len2) return 0; int ans=dp(S,T,len1,len2);
return ans; }
public int dp(String S,String T,int i,int j)
{
if(i<j) return 0;
if(i==0&&j==0) return 1; // "" ""
if(j==0&&i!=0) return 0;//"xxxx" "" if(S.charAt(i-1)==T.charAt(j-1))
{
return dp(S,T,i-1,j-1)+dp(S,T,i-1,j);
}
else return dp(S,T,i-1,j); }
}

2、加入一个矩阵,依然超时

 public class Solution {
public int numDistinct(String S, String T) {
int len1=S.length();
int len2=T.length();
if(len1<len2) return 0;
int d[][]=new int[len1+1][len2+1]; int ans=dp(S,T,len1,len2,d); return ans; }
public int dp(String S,String T,int i,int j,int d[][])
{
if(i<j) return 0; if(i==0&&j==0) return 1; // "" ""
if(i!=0&&j==0) return 0;
if(d[i][j]!=0) return d[i][j]; if(S.charAt(i-1)==T.charAt(j-1))
{
d[i-1][j-1]=dp(S,T,i-1,j-1,d);
d[i-1][j]=dp(S,T,i-1,j,d);
return d[i-1][j-1]+d[i-1][j];
}
else
{
d[i-1][j]=dp(S,T,i-1,j,d);
return d[i-1][j];
} }
}

3.真正的动态规划

 public class Solution {
public int numDistinct(String S, String T) {
int len1=S.length();
int len2=T.length();
if(len1<len2) return 0;
int d[][]=new int[len1+1][len2+1];
for(int i=0;i<=len1;i++)
{
d[i][0]=1;
}
for(int i=1;i<=len1;i++)
{
for(int j=1;j<=len2&&j<=i;j++)
{
if(S.charAt(i-1)==T.charAt(j-1))
{
d[i][j]=d[i-1][j-1]+d[i-1][j];
}
else
{
d[i][j]=d[i-1][j];
} } } return d[len1][len2]; } }

leetcode distinct-subsequences(DP)的更多相关文章

  1. 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)

    引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...

  2. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  3. [leetcode]Distinct Subsequences @ Python

    原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...

  4. [LeetCode] Distinct Subsequences 解题思路

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  5. LeetCode: Distinct Subsequences [115]

    [称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...

  6. LeetCode: Distinct Subsequences 解题报告

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

  7. [LeetCode] Distinct Subsequences [29]

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  8. [Leetcode] distinct subsequences 不同子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  9. Distinct Subsequences (dp)

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  10. Leetcode Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. [DEncrypt] RSACryption--RSA加密/解密字符串 (转载)

    点击下载 RSACryption.zip 这个类是关于加密,解密的操作,文件的一些高级操作1.RSACryption RSA 的密钥产生2.RSACryption RSA的加密函数3.RSACrypt ...

  2. MySQL execute dynamic sql script.

    SET @sql = (SELECT IF( (SELECT COUNT(*) FROM usher_network_log ) > 1000000, "SELECT 0", ...

  3. Java文件File操作一:文件的创建和删除

    一.简述 File 文件类,主要对文件进行相关操作.常用的File操作有:文件(夹)的创建.文件(夹)的删除,文件的读入和下载(复制)等: 二.文件(夹)的创建和删除 1.创建过程 实例: //cre ...

  4. Angularjs总结(五)指令运用及常用控件的赋值操作

    1.常用指令 <div ng-controller="jsyd-controller"> <div style="float:left;width:10 ...

  5. APP启动页

    关于APP启动引导页面模块 时间:2016年6月14日 作者:赵锐 模块使用说明 模块暴露在外的接口是- (void)showGuideViewWithImages:(NSArray *)images ...

  6. 删除svn密码方法

    很多时候使用svn,我们需要切换svn账号,但是由于之前的账号已经选择了记住密码,那么我们应该如何删除svn密码来切换新的svn账号呢? 其实很简单,svn账号密码信息保存在电脑某一文件中,我们只要删 ...

  7. 24种设计模式--桥梁模式【Bridge Pattern】

    今天我要说说我自己,梦想中的我自己,我身价过亿,有两个大公司,一个是房地产公司,一个是服装制造业,这两个公司都很赚钱,天天帮我在累加财富,其实是什么公司我倒是不关心,我关心的是是不是在赚钱,赚了多少, ...

  8. JQ插件ajaxFileUpload、php实现图片,数据同时上传

    代码结构如下: 1.HTML代码,没必要解释了. <!DOCTYPE html> <html> <head> <meta charset="UTF- ...

  9. jquery 核心

    1.jquery核心函数    1.1 jQuery([selector,[context]]);        $("#id"),$(document.body),$(" ...

  10. AngularJS学习-初识

    angularJS定义和特点 1.google前端开源框架 2.MVVM(model view view-model)设计模式 : Model将和ViewModel互动(通过$scope对象),将监听 ...