leetcode distinct-subsequences(DP)
参考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)的更多相关文章
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [leetcode]Distinct Subsequences @ Python
原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...
- [LeetCode] Distinct Subsequences 解题思路
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode: Distinct Subsequences [115]
[称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...
- LeetCode: Distinct Subsequences 解题报告
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- [LeetCode] Distinct Subsequences [29]
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode] distinct subsequences 不同子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences (dp)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- SQL 2012的分页
今天看到一篇文章介绍2012中的分页,就想测试一下新的分页方法比原先的有多少性能的提升,下面是我的测试过程(2012的分页语法这里不在做多的说明,MSDN上一搜就有): 首先我们来构造测试数据: -- ...
- JSP三个指令及9个内置对象
注:本文编写方便自己以后用到时查阅 三大指令:include. page.taglib include指令: 作用: 在当前页面用于包含其他页面 语法: <%@include file=&qu ...
- Linux Bash环境下单引(')、双引号(")、反引号(`)、反斜杠(\)的小结
在bash中,$.*.?.[.].’.”.`.\.有特殊的含义.类似于编译器的预编译过程,bash在扫描命令行的过程中,会在文本层次上,优先解释所有的特殊字符,之后对转换完成的新命令行,进行内核的系统 ...
- Angularjs简介
很久没有系统学习一个新技术了,angularjs将会比较系统的讲解这个技术的语法.应用.次类型的博客将会持续更新,博主也是一个初学者,如果有问题欢迎留言讨论. angularjs简介. angular ...
- [Neural Networks] Momentum
一.目的 加快参数的收敛速度. 二.做法 另第t次的权重更新对第t+1次的权重更新造成影响. 从上式可看出,加入momentum后能够保持权重的更新方向,同时加快收敛.通常alpha的取值为[0.7, ...
- 在本地Apache服务器配置虚拟主机站点
Apache 配置localhost虚拟主机步骤1,打开apache目录下httpd.conf文件,找到如下模块 # Virtual hosts #Include conf ...
- VBA开发经验总结之一:利用Range对象设计用户界面
读罢<EXCEL专业开发>,最大的震撼就是著者对VBA技术的追求以及对Excel艺术品般的设计.受到此书著者的启发,也打算把自己在日常开发中一些经验总结出来,一来作为自己的知识储备,二来也 ...
- Android中用Application类实现全局变量
最近在项目中,遇到了application这个类,开始不知道有什么用,经过学习后才知道它的用途也蛮大的,举个例子,如果想在整个应用中使用全局变量,在java中一般是使用静态变量,public类型:而在 ...
- RPMForge——Quick Start build system
How to setup multimedia on CentOS-5 CentOS ships with basic sound support for audio content encoded ...
- WPF之 XAML集合项简单演示
我们直接通过xaml文件演示一个简单的xaml集合项: <Window x:Class="WPF_XAML集合项.MainWindow" xmlns="http:/ ...