当前位置:网站首页>The sword refers to Offer II 097. Number of subsequences
The sword refers to Offer II 097. Number of subsequences
2022-07-29 21:32:00 【Xiao Lu wants to brush the force and deduct the question】
前言
给定一个字符串 s 和一个字符串 t ,计算在 s 的子序列中 t 出现的个数.
字符串的一个 子序列 是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串.(例如,“ACE” 是 “ABCDE” 的一个子序列,而 “AEC” 不是)
题目数据保证答案符合 32 位带符号整数范围.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/21dk04
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
解题思路
样本对应模型
dp[i][j]
S从0…i的前缀字符串,How many options can be turned intoT的0…jThe number of schemes for this prefix string
Bottom right is the answer
dp[0][0]填1,Represents two empty strings
第一行填0;
第1列都填1
普遍位置
1)不保留i位置的字符
dp[i-1][j]
2) 一定要用的]位置
有条件,要求s[i]== t[j]的情况下,Only this possibility
dp[i][j]+=dp[i-1][j-1]
代码
class Solution {
public int numDistinct(String s, String t) {
char[] str1=s.toCharArray();
char[] str2=t.toCharArray();
int n=str1.length;
int m=str2.length;
int[][] dp=new int[n+1][m+1];
for(int i=0;i<=n;i++){
dp[i][0]=1;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j]=dp[i-1][j]+(str1[i-1]==str2[j-1]?dp[i-1][j-1]:0);
}
}
return dp[n][m];
}
}
边栏推荐
- Oracle问题: ORA-01882: 未找到时区
- JSP Servlet JDBC MySQL CRUD Sample Tutorial
- mysql get field comments and get table fields
- SAG1-MIC8复合DNA基因疫苗|新型脂质-HAP-DNA复合体|实验要求
- 这半年我做交易链路自动化回归的那些事儿...
- 七个易犯的 IT 管理错误—以及如何避免
- 336. 回文对
- Data visualization ---- web page displays temperature and humidity
- Kotlin - 协程作用域 CoroutineScope、协程构建器 CoroutineBuilder、协程作用域函数 CoroutineScope Functiom
- 嵌入式分享合集24
猜你喜欢
怎么实现您的个人知识库?
240. 搜索二维矩阵 II
双功能RGD-TAT修饰DNA纳米胶束|聚苯胺纳米线修饰DNA(PAINW/DNA)
藻酸盐/PEI/DNA复合载体|脂质-鱼精蛋白-DNA复合物|合成方法
Data visualization ---- web page displays temperature and humidity
JUC Concurrent Programming Basics AQS
4D Summary: 38 Knowledge Points of Distributed Systems
In the past six months, I have done those things about the automatic return of the transaction link...
找工作那些事-和表弟的一次聊天
百度实习学弟深夜吐槽:原来大厂是这种生活啊
随机推荐
找工作那些事-和表弟的一次聊天
Single-core browser and what is the difference between dual-core browser, which to use?
conda虚拟环境 | install 与 list 问题
LeetCode 593 有效的正方形[数学] HERODING的LeetCode之路
mysql 获取字段注释 和获取表字段
博世集团启动量子数字孪生计划
JUC并发编程基础AQS
WPF 实现抽屉菜单
Related phrases include usage and collocation (include)
最小jvm源码分析
促进二十一世纪创客教育的新发展
Huawei laptop keyboard locked (how does the laptop keyboard light up)
Baidu internship students late night fun: originally giant is this kind of life
LeetCode_474_ one and zero
单壁碳纳米管-DNA复合物(SWCNT-DNA)|作用机理
378. 有序矩阵中第 K 小的元素
336. 回文对
怎么实现您的个人知识库?
海量数据查询方案mysql_Mysql海量数据存储和解决方案之二—-Mysql分表查询海量数据…[通俗易懂]
Cooler Navigation helps you shop easily in shopping malls without confusion