当前位置:网站首页>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;
}
};
边栏推荐
- 动画与过渡效果
- Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
- 【C 题集】of Ⅶ
- WS2818M是CPC8封装,是三通道LED驱动控制专用电路外置IC全彩双信号5V32灯可编程led灯带户外工程
- SCM polling program framework based on linked list management
- Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)
- HAProxy高可用解决方案
- ViewBinding和DataBinding的理解和区别
- Variable promotion and function promotion in JS
- markdown 语法之字体标红
猜你喜欢
Fisher信息量检测对抗样本代码详解
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
2022年起重机械指挥考试模拟100题模拟考试平台操作
OpenHarmony应用开发之如何创建DAYU200预览器
HAProxy高可用解决方案
面试官:Redis中哈希数据类型的内部实现方式是什么?
使用默认路由作为指向Internet的路由
. Net delay queue
Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
面试拆解:系统上线后Cpu使用率飙升如何排查?
随机推荐
Fs7867s is a voltage detection chip used for power supply voltage monitoring of digital system
C#基础深入学习二
Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
DGraph: 大规模动态图数据集
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
Ws2811 m is a special circuit for three channel LED drive and control, and the development of color light strip scheme
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
Fs4056 800mA charging IC domestic fast charging power IC
XML入门二
锐成芯微冲刺科创板:年营收3.67亿拟募资13亿 大唐电信是股东
FS7867S是一款应用于数字系统供电电源电压监控的电压检测芯片
Node の MongoDB安装
面试官:Redis中哈希数据类型的内部实现方式是什么?
源码编译安装MySQL
【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对
C array supplement
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
remount of the / superblock failed: Permission denied
Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
Golang 使用 JSON unmarshal 数字到 interface{} 数字变成 float64 类型(转)