当前位置:网站首页>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;
}
};边栏推荐
猜你喜欢

近日小结(非技术文)

Node の MongoDB安装

Oracle was named the champion of Digital Innovation Award by Ventana research

1200. Minimum absolute difference

小程序直播 + 电商,想做新零售电商就用它吧!

Commvault 和 Oracle 合作,在 Oracle 云上提供 Metallic数据管理即服务

JVM series - stack and heap, method area day1-2

嵌入式编程中五个必探的“潜在错误”

【R语言数据科学】:交叉验证再回首

JVM 内存布局详解,图文并茂,写得太好了!
随机推荐
C array supplement
【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对
2022g3 boiler water treatment examination question simulation examination question bank and simulation examination
基于STM32+华为云IOT设计的酒驾监控系统
担心“断气” 德国正修改《能源安全法》
C语言宿舍管理查询软件
数据库公共字段自动填充
Understanding and difference between viewbinding and databinding
程序员的焦虑
Haproxy high availability solution
Detailed explanation of Fisher information quantity detection countermeasure sample code
以房抵债能否排除强制执行
. Net using redis
Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire
SCM polling program framework based on linked list management
C语言个人通讯录管理系统
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
How to choose a technology stack for web applications in 2022
Automatic filling of database public fields
2022 hoisting machinery command examination simulation 100 questions simulation examination platform operation