当前位置:网站首页>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;
}
};边栏推荐
- Go 语言入门很简单:Go 实现凯撒密码
- Dgraph: large scale dynamic graph dataset
- 程序员的焦虑
- 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
- 华昊中天冲刺科创板:年亏2.8亿拟募资15亿 贝达药业是股东
- 自主工业软件的创新与发展
- 硬件基础知识-二极管基础
- Source code compilation and installation of MySQL
- Fs7867s is a voltage detection chip used for power supply voltage monitoring of digital system
- C array supplement
猜你喜欢

苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?

1200. Minimum absolute difference

一次 Keepalived 高可用的事故,让我重学了一遍它

Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc

2022KDD预讲 | 11位一作学者带你提前解锁优秀论文

MySQL45讲——学习极客时间MySQL实战45讲笔记—— 06 | 全局锁和表锁_给表加个字段怎么有这么多阻碍

【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value

Interviewer: what is the internal implementation of hash data type in redis?

One of the solutions for unity not recognizing riders

DGraph: 大规模动态图数据集
随机推荐
C语言个人通讯录管理系统
Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
Programmer anxiety
C语言宿舍管理查询软件
2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
程序员的焦虑
中邮科技冲刺科创板:年营收20.58亿 邮政集团是大股东
. Net delay queue
Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
Scripy framework learning
C basic supplement
如何在 2022 年为 Web 应用程序选择技术堆栈
Automatic filling of database public fields
锐成芯微冲刺科创板:年营收3.67亿拟募资13亿 大唐电信是股东
舔狗舔到最后一无所有(状态机)
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 06 | 全局锁和表锁_给表加个字段怎么有这么多阻碍
MongoDB常用28条查询语句(转)
Getting started with the go language is simple: go implements the Caesar password
基于链表管理的单片机轮询程序框架