当前位置:网站首页>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
边栏推荐
- Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
- The four connection methods of JDBC are directly coded
- 1.支付系统
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
- Proceedingjoinpoint API use
- 数字电路基础(五)算术运算电路
- How to earn the first pot of gold in CSDN (we are all creators)
- Don't you even look at such a detailed and comprehensive written software test question?
- 150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview
- Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
猜你喜欢
数字电路基础(二)逻辑代数
Four methods of exchanging the values of a and B
Realize applet payment function with applet cloud development (including source code)
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
Intranet information collection of Intranet penetration (3)
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
Keil5-MDK的格式化代码工具及添加快捷方式
Summary of thread implementation
About the garbled code problem of superstar script
ES全文索引
随机推荐
Proceedingjoinpoint API use
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
Always of SystemVerilog usage_ comb 、always_ iff
Intranet information collection of Intranet penetration (4)
Keil5-MDK的格式化代码工具及添加快捷方式
c语言学习总结(上)(更新中)
王爽汇编语言详细学习笔记二:寄存器
Library management system
With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
Fundamentals of digital circuit (V) arithmetic operation circuit
Function: string storage in reverse order
数字电路基础(四) 数据分配器、数据选择器和数值比较器
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
Intranet information collection of Intranet penetration (3)
Fundamentals of digital circuits (II) logic algebra
Quaternion -- basic concepts (Reprint)
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
【指针】查找最大的字符串