当前位置:网站首页>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;
}
};边栏推荐
- HAProxy高可用解决方案
- CTF competition problem solution STM32 reverse introduction
- 7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
- 安装trinity、解决报错
- Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)
- Oracle 被 Ventana Research 评为数字创新奖总冠军
- 面试官:Redis中哈希数据类型的内部实现方式是什么?
- C foundation in-depth study I
- Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
- Introduction to reverse debugging PE structure resource table 07/07
猜你喜欢

2022g3 boiler water treatment examination question simulation examination question bank and simulation examination

JVM系列——栈与堆、方法区day1-2

字节面试算法题

CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...

源码编译安装MySQL

Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)

Five "potential errors" in embedded programming

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

光环效应——谁说头上有光的就算英雄

OpenHarmony应用开发之如何创建DAYU200预览器
随机推荐
程序员转方向
CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
免费、好用、强大的轻量级笔记软件评测:Drafts、Apple 备忘录、Flomo、Keep、FlowUs、Agenda、SideNote、Workflowy
博士申请 | 西湖大学学习与推理系统实验室招收博后/博士/研究实习等
Introduction to XML I
C語言宿舍管理查詢軟件
Read the BGP agreement in 6 minutes.
Distributed base theory
30: Chapter 3: develop Passport Service: 13: develop [change / improve user information, interface]; (use * * * Bo class to accept parameters, and use parameter verification)
以房抵债能否排除强制执行
C语言程序设计选题参考
微服务入门
Byte interview algorithm question
Commvault 和 Oracle 合作,在 Oracle 云上提供 Metallic数据管理即服务
js中的变量提升和函数提升
Lick the dog until the last one has nothing (state machine)
Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
ASP.NET Core入门一
unity不识别rider的其中一种解决方法
面试拆解:系统上线后Cpu使用率飙升如何排查?