当前位置:网站首页>392. 判断子序列
392. 判断子序列
2022-07-04 12:48:00 【anieoo】
原题链接:392. 判断子序列
solution:
动态规划:
状态表示:dp[i][j]:表示s的前i个字母是否是t的前j的字母的子序列
状态计算: 如果s[i] == t[j]则,dp[i][j] = dp[i - 1][j - 1],否则dp[i][j] = dp[i][j - 1]
class Solution {
public:
bool isSubsequence(string s, string t) {
int m = s.size(),n = t.size();
if(m > n) return false; //m长度大于n一定不是子序列
vector<vector<bool>> dp(m + 1, vector<bool> (n + 1, false));
//dp[i][j]:表示s的前i个字母是否是t的前j的字母的子序列
for(int i = 0;i <= n;i++) dp[0][i] = true;
for(int i = 1;i <= m;i++) {
for(int j = 1;j <= n;j++) {
if(s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = dp[i][j - 1];
}
}
}
return dp[m][n];
}
};双指针,只有s[j] == t[i],指针j才向后移动。
class Solution {
public:
bool isSubsequence(string s, string t) {
int m = s.size(),n = t.size();
if(!m) return true;
if(m > n) return false;
for(int i = 0,j = 0;i < n;i++) {
if(s[j] == t[i]) j++;
if(j == m) return true;
}
return false;
}
};边栏推荐
- 美国土安全部长:国内暴力极端主义是目前美面临的最大恐怖主义威胁之一
- C foundation in-depth study I
- C language programming topic reference
- 2022g3 boiler water treatment examination question simulation examination question bank and simulation examination
- c#数组补充
- C basic supplement
- Install Trinity and solve error reporting
- CommVault cooperates with Oracle to provide metallic data management as a service on Oracle cloud
- 动画与过渡效果
- Go 语言入门很简单:Go 实现凯撒密码
猜你喜欢
Three schemes to improve the efficiency of MySQL deep paging query

Install Trinity and solve error reporting

苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?

2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
![[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source](/img/2c/b1d6277c1b23a6a77f90d5b2874759.png)
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source

ASP. Net core introduction I

面试拆解:系统上线后Cpu使用率飙升如何排查?

2022G3锅炉水处理考试题模拟考试题库及模拟考试

源码编译安装MySQL

安装trinity、解决报错
随机推荐
How to choose a technology stack for web applications in 2022
Commvault 和 Oracle 合作,在 Oracle 云上提供 Metallic数据管理即服务
XILINX/system-controller-c/BoardUI/无法连接开发板,任意操作后卡死的解决办法
面试拆解:系统上线后Cpu使用率飙升如何排查?
The Secretary of Homeland Security warned immigrants "not to embark on a dangerous journey"
嵌入式编程中五个必探的“潜在错误”
Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
ASP. Net core introduction I
#yyds干货盘点# 解决名企真题:连续最大和
Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
硬件基础知识-二极管基础
基于链表管理的单片机轮询程序框架
Oracle was named the champion of Digital Innovation Award by Ventana research
MySQL 45 lecture - learn the actual combat notes of MySQL in Geek time 45 lecture - 06 | global lock and table lock_ Why are there so many obstacles in adding a field to the table
ViewBinding和DataBinding的理解和区别
WPF double slider control and forced capture of mouse event focus
2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance
SQL language
C语言宿舍管理查询软件