当前位置:网站首页>392. 判断子序列
392. 判断子序列
2022-07-04 12:48:00 【anieoo】
原题链接:392. 判断子序列
solution:
动态规划:
状态表示:dp[i][j]:表示s的前i个字母是否是t的前j的字母的子序列
状态计算: 如果s[i] == t[j]则,dp[i][j] = dp[i - 1][j - 1],否则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长度大于n一定不是子序列
vector<vector<bool>> dp(m + 1, vector<bool> (n + 1, false));
//dp[i][j]:表示s的前i个字母是否是t的前j的字母的子序列
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];
}
};
双指针,只有s[j] == t[i],指针j才向后移动。
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;
}
};
边栏推荐
- OpenHarmony应用开发之如何创建DAYU200预览器
- SCM polling program framework based on linked list management
- Reading cognitive Awakening
- JVM系列——栈与堆、方法区day1-2
- ViewBinding和DataBinding的理解和区别
- unity不识别rider的其中一种解决方法
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- Optional values and functions of the itemized contenttype parameter in the request header
- Web知识补充
- C语言个人通讯录管理系统
猜你喜欢
苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?
OPPO Find N2产品形态首曝:补齐各项短板
CommVault cooperates with Oracle to provide metallic data management as a service on Oracle cloud
Go 语言入门很简单:Go 实现凯撒密码
Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
如何在 2022 年为 Web 应用程序选择技术堆栈
Oracle 被 Ventana Research 评为数字创新奖总冠军
Understanding and difference between viewbinding and databinding
Oracle was named the champion of Digital Innovation Award by Ventana research
2022 hoisting machinery command examination simulation 100 questions simulation examination platform operation
随机推荐
Don't turn down, three sentences to clarify the origin of cross domain resource request errors
舔狗舔到最后一无所有(状态机)
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
ASP.NET Core入门一
Fs7867s is a voltage detection chip used for power supply voltage monitoring of digital system
分布式BASE理论
C语言图书租赁管理系统
Haproxy high availability solution
Introduction to XML I
C语言职工管理系统
Web knowledge supplement
Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
PostgreSQL 9.1 soaring Road
7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
30: Chapter 3: develop Passport Service: 13: develop [change / improve user information, interface]; (use * * * Bo class to accept parameters, and use parameter verification)
Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
Summary of recent days (non-technical article)
2022 hoisting machinery command examination simulation 100 questions simulation examination platform operation
面试拆解:系统上线后Cpu使用率飙升如何排查?
C language programming topic reference