【LeetCode】115.Distinct Subsequences
来源:互联网 发布:崛起网络f18团平台图片 编辑:程序博客网 时间:2024/06/05 04:39
Description:
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
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).
example:
S = "rabbbit"
, T = "rabbit"
Return 3
.
题目思考:
题目给定两个字符串,只可以用删除字符的方法从第一个字符串变换到第二个字符串,求出一共有多少种变换方法;
这是动态规划思想常见应用的典型题目。
Solution:
class Solution {
public:
int numDistinct(string s, string t) {
int len = t.length();
int *memo = new int[len + 1];
for (int i = 0; i < len + 1; i++) {
memo[i] = 0;
}
memo[0] = 1;
for (int i = 1; i < s.length() + 1; i++) {
for (int j = len - 1; j >= 0; j--) {
memo[j + 1] += s[i - 1] == t[j] ? memo[j] : 0;
}
}
return memo[len];
}
};
int main(int argc, const char * argv[]) {
Solution s;
cout << s.numDistinct("rabbbit", "rabbit") << endl;
return 0;
}
public:
int numDistinct(string s, string t) {
int len = t.length();
int *memo = new int[len + 1];
for (int i = 0; i < len + 1; i++) {
memo[i] = 0;
}
memo[0] = 1;
for (int i = 1; i < s.length() + 1; i++) {
for (int j = len - 1; j >= 0; j--) {
memo[j + 1] += s[i - 1] == t[j] ? memo[j] : 0;
}
}
return memo[len];
}
};
int main(int argc, const char * argv[]) {
Solution s;
cout << s.numDistinct("rabbbit", "rabbit") << endl;
return 0;
}
阅读全文
0 0
- LeetCode 115. Distinct Subsequences
- [Leetcode] 115. Distinct Subsequences
- [leetcode] 115.Distinct Subsequences
- [leetcode] 115.Distinct Subsequences
- Leetcode 115. Distinct Subsequences
- 【LeetCode】115. Distinct Subsequences
- LeetCode 115. Distinct Subsequences
- [leetcode]115. Distinct Subsequences
- LeetCode-115.Distinct Subsequences
- [LeetCode] 115. Distinct Subsequences
- LeetCode 115. Distinct Subsequences
- leetcode-115. Distinct Subsequences
- leetcode-115. Distinct Subsequences
- [LeetCode]115. Distinct Subsequences
- 【Leetcode】115. Distinct Subsequences
- [LeetCode] 115. Distinct Subsequences
- leetcode:115. Distinct Subsequences
- LeetCode 115. Distinct Subsequences
- hbase简单分析及其命令
- 394. Decode String
- 1047. 编程团体赛(20)
- JDk目录介绍
- Linux下的进程概论与编程三(进程间通信的5种方式)
- 【LeetCode】115.Distinct Subsequences
- 通熟易懂学习卷积神经网络(CNN)
- Derek的成长之路——前端与移动开发之HTML(基础篇)
- 封装好的webview
- 营创学院 肖知兴 分享课
- iptables只允许指定ip访问本机的指定端口
- 【代码笔记】iOS-Transition动画
- python爬虫系列教程,静谧
- C# 发送POST请求