当前位置:网站首页>Cc36 different subsequences
Cc36 different subsequences
2022-07-06 14:48:00 【sakeww】
link :
Niuke link :
https://www.nowcoder.com/practice/ed2923e49d3d495f8321aa46ade9f873?tpId=46&tqId=29065&tPage=1&rp=1&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking
leetcode link :
https://leetcode-cn.com/problems/distinct-subsequences/
Description and examples :
Code :
Method 1 :
class Solution {
public:
int numDistinct(string S, string T)
{
int row = S.size();
int col = T.size();
vector<vector<int>> arr(row + 1, vector<int>(col + 1));
arr[0][0] = 1;
for (int i = 1; i <= row; ++i)
{
arr[i][0] = 1;
for (int j = 1; j <= col; ++j)
{
if (S[i - 1] == T[j - 1])
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
else
arr[i][j] = arr[i - 1][j];
}
}
return arr[row][col];
}
};
Method 1 Analysis :
The final result is shown in the figure :
Method 2 :
class Solution {
public:
int numDistinct(string S, string T)
{
int row = S.size();
int col = T.size();
vector<int> arr(col + 1);
arr[0] = 1;
for (int i = 1; i <= row; ++i)
{
for (int j = col; j > 0; --j)
{
if (S[i - 1] == T[j - 1])
arr[j] = arr[j - 1] + arr[j];
}
}
return arr[col];
}
};
Method 2 analysis :
Because we only use one row of data for the two-dimensional array in method 1
When traversing to a point , The value of this point is related to the previous one :
When the same : Equal to its own value plus the previous value
When different : Equal to its own value
边栏推荐
- MySQL learning notes (stage 1)
- “Hello IC World”
- High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
- Solutions to common problems in database development such as MySQL
- 指针--剔除字符串中的所有数字
- Pointers: maximum, minimum, and average
- Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
- Realize applet payment function with applet cloud development (including source code)
- About the garbled code problem of superstar script
- Intranet information collection of Intranet penetration (2)
猜你喜欢
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
Database monitoring SQL execution
《统计学》第八版贾俊平第八章假设检验知识点总结及课后习题答案
Fundamentals of digital circuits (III) encoder and decoder
Uibutton status exploration and customization
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
5 minutes to master machine learning iris logical regression classification
Wang Shuang's detailed learning notes of assembly language II: registers
Wang Shuang's detailed notes on assembly language learning I: basic knowledge
JDBC transactions, batch processing, and connection pooling (super detailed)
随机推荐
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
c语言学习总结(上)(更新中)
To brush the video, it's better to see if you have mastered these interview questions. Slowly accumulating a monthly income of more than 10000 is not a dream.
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
How to learn automated testing in 2022? This article tells you
《统计学》第八版贾俊平第四章总结及课后习题答案
函数:计算字符串中大写字母的个数
[pointer] solve the last person left
flask实现强制登陆
[pointer] use the insertion sorting method to arrange n numbers from small to large
150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview
《统计学》第八版贾俊平第九章分类数据分析知识点总结及课后习题答案
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
【指针】删除字符串s中的所有空格
函数:求两个正数的最大公约数和最小公倍
[pointer] the array is stored in reverse order and output
《统计学》第八版贾俊平第二章课后习题及答案总结
Get started with Matplotlib drawing
Intranet information collection of Intranet penetration (4)
【指针】求二维数组中最大元素的值