【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Given a string S and a string T, count the number of distinct subsequences of T in S.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ACE” is a subsequence of “ABCDE” while “AEC” is not).
Here is an example:
S = “rabbbit”, T = “rabbit”Return 3.
(二)解题
题目大意:给定字符串T和S,求S的子串中有多少与T相等。
解题思路:S的子串代表在S中删除一些字符组成的字符串,那么可以很容易想到用递归来解决。
如果当前s[i]==s[j],有两种情况,选择该字母(i++,j++)和跳过该字母(i++)
如果s[i]!=s[j],则直接i++.
具体见代码:
class Solution {
public:
int numDistinct(string s, string t) {
if(s.length()==0||t.length()==0) return 0;
int num =0;
dpDistinct(s,t,0,0,num);
return num;
}
void dpDistinct(string& s, string& t , int i , int j,int& num)
{
if(j==t.length()){num++;return;}
if(s[i]==t[j])
{
//choose this words
if(i<s.length()&&j<t.length()) dpDistinct(s, t , i+1, j+1,num);
//not choose this words
if(i<s.length()) dpDistinct(s, t , i+1, j,num);
}
else
//Don't equal
if(i<s.length()) dpDistinct(s, t , i+1, j,num);
}
};
然后……直接超时了。
观察上述代码,发现有很多重复的判断,因此,可以采用动态规划的思想。
开始找状态转移方程。dp[i][j]用来表示s中j之前的子串subs中有多少个不同的subt,其中subt为t中i之前的字符组成的子串。
首先,初始化dp,令dp[0][j]都等于1,因为s中删除所有的字符都能组成空串。
如果t[i] == s[j],那么dp[i][j] = dp[i][j-1]+dp[i-1][j-1]
否则,dp[i][j] = dp[i][j-1]
举例说明一下:s为abbbc,t为abc
| dp | 空 | a | b | b | b | c |
|---|---|---|---|---|---|---|
| 空 | 1 | 1 | 1 | 1 | 1 | 1 |
| a | 0 | 1 | 1 | 1 | 1 | 1 |
| b | 0 | 0 | 1 | 2 | 3 | 3 |
| c | 0 | 0 | 0 | 0 | 0 | 3 |
具体解释看代码:
class Solution {
public:
int numDistinct(string s, string t) {
vector<vector<int>> dp;
for(int i = 0 ; i < t.length()+1;i++)
{
vector<int> temp(s.length()+1,0);
dp.push_back(temp);
}
for(int i = 0 ; i < s.length()+1;i++) dp[0][i]=1;//初始化dp
for(int i = 1 ; i < t.length()+1; i++)
{
for(int j = 1 ; j < s.length()+1 ; j++)
{
if(s[j-1]==t[i-1])
{
dp[i][j] = dp[i-1][j-1]+dp[i][j-1];//状态转移方程
}
else
dp[i][j] = dp[i][j-1];
}
}
return dp[t.length()][s.length()];//返回
}
};
【一天一道LeetCode】#115. Distinct Subsequences的更多相关文章
- [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 ...
- [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 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 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- Leetcode#115 Distinct Subsequences
原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 115. Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- YOLO: 3 步实时目标检测安装运行教程 [你看那条狗,好像一条狗!]
封面图是作者运行图,我在 ubuntu 环境下只有文字预测结果. Detection Using A Pre-Trained Model 使用训练好的模型来检测物体 运行一下命令来下载和编译模型 gi ...
- # electron-vue 尝试做个网易云音乐
当跑起来electron第一刻 我发现这个浏览器头是不是有点丑 是不是可以隐藏起来呢,答案当然是可以的 src/main/index.js mainWindow = new BrowserWindow ...
- go优化
1.安装graphviz软件,安装步骤如下::(1)安装graphvizyum install 'graphviz*' (2)查看已安装graphviz包yum list 'graphviz*' 2. ...
- 含有分类变量(categorical variable)的逻辑回归(logistic regression)中虚拟变量(哑变量,dummy variable)的理解
版权声明:本文为博主原创文章,博客地址:,欢迎大家相互转载交流. 使用R语言做逻辑回归的时候,当自变量中有分类变量(大于两个)的时候,对于回归模型的结果有一点困惑,搜索相关知识发现不少人也有相同的疑问 ...
- ACM 人见人爱A^B
求A^B的最后三位数表示的整数. 说明:A^B的含义是"A的B次方" Input输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10 ...
- HTML DOM 改变 HTML 内容
HTML DOM 允许 JavaScript 改变 HTML 元素的内容. 改变 HTML 输出流 JavaScript 能够创建动态的 HTML 内容: 今天的日期是: Thu Feb 25 201 ...
- 状态模式、职责链模式——省去if-else的繁琐结构
小时候写日记都是这么写的:上午七点起床,八点之前洗脸刷牙吃早饭,十二点之前好好上课,中午一点,吃午饭,下午两点到六点,上课,下课,找请假,明天妈妈要带我去姥姥家,九点之前,看动画片,九点钟,收拾去姥姥 ...
- Bootstrap3 代码-代码块
多行代码可以使用 <pre> 标签.为了正确的展示代码,注意将尖括号做转义处理. <p>Sample text here...</p> <pre>< ...
- Python 2.7 闭包的局限
想法源自:http://stackoverflow.com/questions/141642/what-limitations-have-closures-in-python-compared-to- ...
- SimpleDateFormat中parse和format的区别
parse()返回的是一个Date类型数据,format返回的是一个StringBuffer类型的数据 //SimpleDateFormat中的parse方法可以 //把String型的字符串转换成特 ...