LeetCode OJ-- Distinct Subsequences ** 递推
https://oj.leetcode.com/problems/distinct-subsequences/
对于string S 和 T求,T 是 S的几种子串。
首先想到了递归方法,列出递归公式,奈何超时了:
如果S[sBegin] == T[tBegin] 则是 numSubDistinct(sBegin+1,tBegin+1,S,T) + numSubDistinct(sBegin+1,tBegin,S,T);
如果不等于,则 numSubDistinct(sBegin+1,tBegin,S,T);
如果T的遍历指针到头了,则说明成功了一次匹配
class Solution {
public:
int numDistinct(string S, string T) {
int ans = ;
if(S.size()<T.size())
return ;
if(S.size() == T.size())
if(S == T)
return ;
else
return ;
return numSubDistinct(,,S,T);
}
int numSubDistinct(int sBegin,int tBegin,string &S,string &T)
{
if(tBegin== T.size())
return ;
if(sBegin == S.size() && tBegin!= T.size())
return ;
if(S[sBegin] == T[tBegin])
return numSubDistinct(sBegin+,tBegin+,S,T) + numSubDistinct(sBegin+,tBegin,S,T);
else
return numSubDistinct(sBegin+,tBegin,S,T);
}
};
之后,考虑用递推实现。
根据思路来说,首先想到从后往前递推,但如果从后往前可以,则从前往后也可以。
建立二维数组num[S.size()][T.size()]存这个过程中产生的中间结果。
首先定义:num[i][j]是对于S从0到 i 的子串,对于T从 0 到 j 的子串,这两个字符串,T是S的子串数。
如果S[sBegin] == T[tBegin] num[sBegin][tBegin] = num[sBegin-1][tBegin-1] + num[sBegin-1][tBegin];
如果不等于则 num[sBegin][tBegin] = num[sBegin-1][tBegin];
根据公式,考虑到进行两层循环,
但是循环之前需要初始化
class Solution {
public:
int numDistinct(string S, string T) {
int ans = ;
if(S.size()<T.size())
return ;
if(S.size() == T.size())
if(S == T)
return ;
else
return ; vector<vector<int> > num;
num.resize(S.size());
for(int i = ;i<S.size();i++)
num[i].resize(T.size()); //initialize
if(S[] == T[])
num[][] = ;
else
num[][] = ;
for(int i = ;i<S.size();i++)
{
if(S[i] == T[])
num[i][] = num[i-][] + ;
else
num[i][] = num[i-][];
} for(int j = ;j<T.size();j++)
{
num[][j] = ;
} for(int sBegin = ;sBegin<S.size();sBegin++)
for(int tBegin = ;tBegin<T.size();tBegin++)
{
if(S[sBegin] == T[tBegin])
num[sBegin][tBegin] = num[sBegin-][tBegin-] + num[sBegin-][tBegin];
else
num[sBegin][tBegin] = num[sBegin-][tBegin];
} return num[S.size()-][T.size()-];
}
};
LeetCode OJ-- Distinct Subsequences ** 递推的更多相关文章
- [LeetCode OJ] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [LeetCode] 115. Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- Java for LeetCode 115 Distinct Subsequences【HARD】
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [Leetcode][JAVA] 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(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- leetcode 115 Distinct Subsequences ----- java
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode 70 - 爬楼梯 - [递推+滚动优化]
假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2输出: 2解释: 有两种方 ...
- [leetcode]115. Distinct Subsequences 计算不同子序列个数
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- 九度OJ 1081:递推数列 (递归,二分法)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6194 解决:864 题目描述: 给定a0,a1,以及an=p*a(n-1) + q*a(n-2)中的p,q.这里n >= 2. 求第 ...
随机推荐
- Darwin's Letter【达尔文的信】
Darwin's Letter A letter written by Charles Darwin in 1875 has been returned to the Smithsonian Inst ...
- POJ 3414 BFS 输出过程
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17456 Accepted: 7407 Special J ...
- shell脚本中的交互式输入自动化
shell中有时我们需要交互,但是呢我们又不想每次从stdin输入,想让其自动化,这时我们就要使shell交互输入自动化了. 1 利用重定向 重定向的方法应该是最简单的 例: 以下的te ...
- 菜鸟学Linux - bash的配置文件
bash是各大Linux发行版都支持的shell.当我们登陆bash的时候,虽然我们什么都没做,但是我们已经可以在bash中调用各种各样的环境变量了.这是因为,系统中已经定义了一系列的配置文件,以及加 ...
- 我给女朋友讲编程CSS系列(1) –添加CSS样式的3种方式及样式表的优先权
如果说,原生态就是美,那么,我们就没有必要穿衣打扮. 网页是什么? 说白了,网页就是一堆[html标签]有序的搭配,让[CSS属性值]整整容,请[Javascript语言]处理一下事件. 一个人的整容 ...
- 设计模式之第2章-抽象工厂模式(Java实现)
设计模式之第2章-抽象工厂模式(Java实现) “上次是我的不对,贿赂作者让我先讲来着,不过老婆大人大人有大量,不与我计较,这次还让我先把上次未讲完的应用场景部分给补充上去,有妻如此,夫复何求.”(说 ...
- jquery ajax return jsonresult pattern
//javascriptvar queryParams = { "userId": userId, "factoryId": factoryId } ...
- python - 接口自动化测试 - ReadConfig - 读取配置文件封装
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: read_config.py @ide: PyCharm ...
- 10 Java 对象的内存布局
Java 创建对象的方式 1:new 语句和反射机制创建.该方式会调用类的构造器,同时满足诸多约束.如果一个类没有构造器的话,Java 编译器会自动添加一个无参数的构造器.子类的构造器需要调用父类的构 ...
- Map 中的EntrySet() ,Map的遍历
我们循环Map时一般用到EntrySet(),EntrySet() 返回的时Set集合(Set<Map.Entry<K, V>>). 那么这里的有Map.Entry< ...