当前位置:网站首页>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;
}
};
边栏推荐
- Getting started with microservices
- 光环效应——谁说头上有光的就算英雄
- After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
- Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
- CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
- C foundation in-depth study I
- [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
- SQL language
- 基于STM32+华为云IOT设计的酒驾监控系统
- C语言程序设计
猜你喜欢
面试拆解:系统上线后Cpu使用率飙升如何排查?
2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance
Animation and transition effects
Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
How to choose a technology stack for web applications in 2022
2022g3 boiler water treatment examination question simulation examination question bank and simulation examination
One of the solutions for unity not recognizing riders
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)
unity不识别rider的其中一种解决方法
随机推荐
C language staff management system
ViewBinding和DataBinding的理解和区别
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
2022G3锅炉水处理考试题模拟考试题库及模拟考试
C語言宿舍管理查詢軟件
C语言图书租赁管理系统
Scripy framework learning
AI painting minimalist tutorial
C语言职工管理系统
美国土安全部部长警告移民“不要踏上危险的旅程”
[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
基于链表管理的单片机轮询程序框架
Fisher信息量检测对抗样本代码详解
Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
Introduction to XML II
Runc hang causes the kubernetes node notready
8 expansion sub packages! Recbole launches 2.0!
C foundation in-depth study I