当前位置:网站首页>不同的子序列问题I
不同的子序列问题I
2022-06-29 00:46:00 【GreyZeng】
不同的子序列问题I
作者:Grey
原文地址: 不同的子序列问题I
题目链接
暴力解法
定义递归函数
int process(char[] str, char[] t, int i, int j)
递归函数表示:str从i一直到最后,生成的序列可以匹配多少个t从j往后生成的字符串
所以process(str,t,0,0)得到的结果就是答案。
接下来考虑递归函数的base case,
if (j == t.length) {
// 表示str已经把t整个都搞定了,返回1,说明得到了一种情况
return 1;
}
// 到了这里,说明t还没到头
if (i == str.length) {
// str已经没有字符串了,t又没到头,所以,无法匹配
return 0;
}
接下来是普遍位置,考虑str[i]是否参与匹配来决定下一步的操作,注:str[i]如果要参与匹配,则必须满足str[i] == t[j]。
// str[i]位置不参与匹配
int ans = process(str, t, i + 1, j);
if (str[i] == t[j]) {
// str[i]参与,必须满足str[i] == t[j]
ans += process(str, t, i + 1, j + 1);
}
完整代码如下
public static int numDistinct(String s, String t) {
if (s.length() < t.length()) {
return 0;
}
return process(s.toCharArray(), t.toCharArray(), 0, 0);
}
// str[0....结尾]搞定t[0....结尾]
public static int process(char[] str, char[] t, int i, int j) {
if (j == t.length) {
// 全部搞定了
return 1;
}
if (i == str.length) {
// 没有了,搞不定
return 0;
}
// 不用i位置的去搞定
int ans = process(str, t, i + 1, j);
if (str[i] == t[j]) {
ans += process(str, t, i + 1, j + 1);
}
return ans;
}
这个暴力解法在LeetCode上直接超时。
动态规划
二维数组
根据暴力方法,可以得到,递归函数只有两个可变参数,所以定义二维dp,dp的含义和递归函数的含义保持一致。所以dp[0][0]就是答案。
int m = str.length;
int n = target.length;
int[][] dp = new int[m + 1][n + 1];
根据暴力方法
if (j == t.length) {
// 全部搞定了
return 1;
}
if (i == str.length) {
// 没有了,搞不定
return 0;
}
可以得到dp的最后一行都是1,即
for (int i = 0; i < m + 1; i++) {
dp[i][n] = 1;
}
接下来考虑普遍的dp[i][j],根据暴力方法
int ans = process(str, t, i + 1, j);
if (str[i] == t[j]) {
ans += process(str, t, i + 1, j + 1);
}
可以得到,dp[i][j]依赖dp[i+1][j]和dp[i+1][j+1](需要满足str[i] == t[j])位置的值。
所以
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
dp[i][j] = dp[i + 1][j] + (str[i] == target[j] ? dp[i + 1][j + 1] : 0);
}
}
完整代码
public static int numDistinct(String s, String t) {
if (s.length() < t.length()) {
return 0;
}
char[] str = s.toCharArray();
char[] target = t.toCharArray();
int m = str.length;
int n = target.length;
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i < m + 1; i++) {
dp[i][n] = 1;
}
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
dp[i][j] = dp[i + 1][j] + (str[i] == target[j] ? dp[i + 1][j + 1] : 0);
}
}
return dp[0][0];
}
时间复杂度O(m*n),其中m和n分别是s和t的长度。
空间复杂度O(m*n),其中m和n分别是s和t的长度。
一维数组
通过分析上述动态规划的解法,我们可得到一个结论,二维dp的计算顺序是从最后一行到第一行,且当前行只依赖上一行有限几个位置的信息,所以,我们可以将上述二维表简化成一维表,定义
int m = str.length;
int[] dp = new int[n + 1];
通过一维表的从最后一行到第一行的滚动更新,来得到第一行的值,完整代码如下
public static int numDistinct(String s, String t) {
if (s.length() < t.length()) {
return 0;
}
char[] str = s.toCharArray();
char[] target = t.toCharArray();
int m = str.length;
int n = target.length;
int[] dp = new int[n + 1];
dp[n] = 1;
for (int i = m - 1; i >= 0; i--) {
// 这里要注意,从左往右
for (int j = 0; j <= n - 1; j++) {
dp[j] += (str[i] == target[j] ? dp[j + 1] : 0);
}
}
return dp[0];
}
时间复杂度O(m*n),其中m和n分别是s和t的长度。
空间复杂度O(n),其中n是t的长度。
更多
边栏推荐
- Is pension insurance a financial product? Where is the expected return?
- [Architect (Part 38)] locally install the latest version of MySQL database developed by the server
- Browser cache library design summary (localstorage/indexeddb)
- Encapsulation of JDBC connection and disconnection database
- UI highly adaptive modification scheme
- Reference materials in the process of using Excel
- Sampling with VerilogA module
- Report on the convenient bee Lantern Festival: the first peak sales of pasta products this year; prefabricated wine dumplings became the winners
- 【leetcode】1719. Number of schemes for reconstructing a tree
- [image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
猜你喜欢

使用.Net驱动Jetson Nano的OLED显示屏

pinhole camera model

Accessories and working process of machine vision system

Structure of the actual combat battalion | module 5

企业和IT领导者对创新的误解

Analysis Framework -- establishment of user experience measurement data system

What is contemporaneous group analysis? Teach you to use SQL to handle
![[leetcode] 522. Longest special sequence II violence + double pointer](/img/88/3ddeefaab7e29b8eeb412bb5c3e9b8.png)
[leetcode] 522. Longest special sequence II violence + double pointer
How does the JVM bottom layer implement synchronized

滑环电机是如何工作的
随机推荐
How does the JVM bottom layer implement synchronized
个人买同业存单基金选择什么证券公司开户好,更安全
It is safer for individuals to choose a securities company to open an account when buying interbank certificates of deposit
Baidu online disk login verification prompt: unable to access this page, or the QR code display fails, the pop-up window shows: unable to access this page, ensure the web address....
[image detection] line recognition based on Hough transform (fitting angle bisector) with matlab code
[200 opencv routines] 101 adaptive median filter
Two ways to set up a single Nacos load balancing ribbon polling policy weight
12. object detection mask RCNN
MySQL high availability dual master synchronization
由背景图缓存导致的canvas绘图跨域问题
手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??
[image detection] recognition of the front and back of a coin based on texture features with matlab code attached
Roson's QT journey 80 qurl class
[staff] pedal mark (step on pedal ped mark | release pedal * mark | corresponding pedal command in MIDI | continuous control signal | switch control signal)
浏览器缓存库设计总结(localStorage/indexedDB)
Structure of the actual combat battalion | module 5
Use and principle of handlerthread
Nodejs installation and download
674. longest continuous increasing sequence
SCP copy folder