参考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. spring配置文件中属性mappingLocations、mappingDirectoryLocations

    http://blog.csdn.net/vacblog/article/details/7774173

  2. Css 梯形图形 并添加文字

    HTML页面的代码: <body> <div style="width:500px;border:solid 1px #ccc;"> <div> ...

  3. java开发规范总结_代码注释规范

    规范需要平时编码过程中注意,是一个慢慢养成的好习惯 1.基本规则 1.注释应该使代码更加清晰易懂   2.注释要简单明了,只要提供能够明确理解程序所必要的信息就可以了.如果注释太复杂说明程序需要修改调 ...

  4. iOS SDwebImage 使用说明

    SDWebImage托管在github上.https://github.com/rs/SDWebImage 这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下 ...

  5. ios framework通用库的制作

    这篇文章是在史上最完整的iOS DIY framework 详细教程(一)的基础上加以修改 1.新建一个静态库工程: 2:取自己喜欢的名字: 3.删除向导所生成工程中的 Target: 3.删除Tes ...

  6. (转) c# ExecuteNonQuery() 返回值 -1

    这是之前我遇到问题,在网上找解决方法时找到的,当时复制到txt文档了,今天整理笔记又看到了,贴出来,便于以后查阅.原文的作者没记住~~ 查询某个表中是否有数据的时候,如果用ExecuteNonQuer ...

  7. Javascript字符串拼接小技巧

    在Javascript中经常会遇到字符串的问题,但是如果要拼接的字符串过长就比较麻烦了. 如果是在一行的,可读性差不说,如果要换行的,会直接报错. 在此介绍几种Javascript拼接字符串的技巧. ...

  8. POJ 1936 All in All(模拟)

    All in All 题目链接:http://poj.org/problem?id=1936 题目大意:判断从字符串s2中能否找到子串s1.字符串长度为10W. Sample Input sequen ...

  9. Codevs 1010 过河卒 2002年NOIP全国联赛普及组

    1010 过河卒 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 如图,A 点有一个过河卒 ...

  10. 要想重启后也生效LINUX防火墙配置

    新配置的一台服务器,安装的是CentOS6.3系统,在安装完LNMP之后,发现nginx进程存在,且php解析正常,但是用分配的独立IP去访问的时候发现无法访问. 查了下网上的资料,发现可能是Linu ...