当前位置:网站首页>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;
}
};
边栏推荐
- Web知识补充
- C#基础深入学习二
- Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
- xshell/bash/zsh 等终端鼠标滚轮乱码问题(转)
- C#基础深入学习一
- Source code compilation and installation of MySQL
- Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
- 使用默认路由作为指向Internet的路由
- Oracle 被 Ventana Research 评为数字创新奖总冠军
- C语言程序设计选题参考
猜你喜欢
30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)
华昊中天冲刺科创板:年亏2.8亿拟募资15亿 贝达药业是股东
392. 判断子序列
基于链表管理的单片机轮询程序框架
Haproxy high availability solution
2022 Shandong Province safety officer C certificate examination question bank and online simulation examination
高质量软件架构的唯一核心指标
一次 Keepalived 高可用的事故,让我重学了一遍它
HAProxy高可用解决方案
Interview disassembly: how to check the soaring usage of CPU after the system goes online?
随机推荐
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
C language dormitory management query software
OpenHarmony应用开发之如何创建DAYU200预览器
Fs7867s is a voltage detection chip used for power supply voltage monitoring of digital system
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
Gorm 读写分离(转)
基于链表管理的单片机轮询程序框架
担心“断气” 德国正修改《能源安全法》
[C question set] of VII
.NET 使用 redis
逆向调试入门-PE结构-资源表07/07
【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value
小程序直播 + 电商,想做新零售电商就用它吧!
结合案例:Flink框架中的最底层API(ProcessFunction)用法
2022 Shandong Province safety officer C certificate examination question bank and online simulation examination
Samsung's mass production of 3nm products has attracted the attention of Taiwan media: whether it can improve the input-output rate in the short term is the key to compete with TSMC
英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
Five "potential errors" in embedded programming
C foundation in-depth learning II
舔狗舔到最后一无所有(状态机)