当前位置:网站首页>392. Judgement subsequence
392. Judgement subsequence
2022-07-04 14:01:00 【anieoo】
Original link :392. Judging subsequences
solution:
Dynamic programming :
State means :dp[i][j]: Express s Before i Are the letters yes t Before j Subsequence of letters of
State calculation : If s[i] == t[j] be ,dp[i][j] = dp[i - 1][j - 1], otherwise 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 Longer than n It must not be a subsequence
vector<vector<bool>> dp(m + 1, vector<bool> (n + 1, false));
//dp[i][j]: Express s Before i Are the letters yes t Before j Subsequence of letters of
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];
}
};
Double pointer , Only s[j] == t[i], The pointer j To move backwards .
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;
}
};
边栏推荐
- ViewBinding和DataBinding的理解和区别
- 華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
- Qt如何实现打包,实现EXE分享
- Oracle 被 Ventana Research 评为数字创新奖总冠军
- CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
- Web知识补充
- Introduction to XML II
- Ws2811 m is a special circuit for three channel LED drive and control, and the development of color light strip scheme
- mac redis安装与使用,连接远程服务器 redis
- 舔狗舔到最后一无所有(状态机)
猜你喜欢
Qt如何实现打包,实现EXE分享
Byte interview algorithm question
Animation and transition effects
205. 同构字符串
Install Trinity and solve error reporting
国内酒店交易DDD应用与实践——代码篇
Summary of recent days (non-technical article)
MySQL version 8 installation Free Tutorial
One of the solutions for unity not recognizing riders
MySQL5免安装修改
随机推荐
Interviewer: what is the internal implementation of hash data type in redis?
2022G3锅炉水处理考试题模拟考试题库及模拟考试
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 06 | 全局锁和表锁_给表加个字段怎么有这么多阻碍
数据库公共字段自动填充
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
1200. Minimum absolute difference
go vendor 项目迁移到 mod 项目
嵌入式编程中五个必探的“潜在错误”
WS2818M是CPC8封装,是三通道LED驱动控制专用电路外置IC全彩双信号5V32灯可编程led灯带户外工程
【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value
基于STM32+华为云IOT设计的酒驾监控系统
ASP. Net core introduction I
Getting started with the go language is simple: go implements the Caesar password
面试拆解:系统上线后Cpu使用率飙升如何排查?
C语言宿舍管理查询软件
Fs4056 800mA charging IC domestic fast charging power IC
.NET 使用 redis
Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
JVM 内存布局详解,图文并茂,写得太好了!
Gorm 读写分离(转)