当前位置:网站首页>剑指 Offer 19. 正则表达式匹配-递归
剑指 Offer 19. 正则表达式匹配-递归
2022-06-09 09:28:00 【Mr Gao】
剑指 Offer 19. 正则表达式匹配
请实现一个函数用来匹配包含’. ‘和’‘的正则表达式。模式中的字符’.‘表示任意一个字符,而’'表示它前面的字符可以出现任意次(含0次)。在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但与"aa.a"和"ab*a"均不匹配。
示例 1:
输入:
s = “aa”
p = “a”
输出: false
解释: “a” 无法匹配 “aa” 整个字符串。
示例 2:
输入:
s = “aa”
p = “a*”
输出: true
解释: 因为 ‘*’ 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 ‘a’。因此,字符串 “aa” 可被视为 ‘a’ 重复了一次。
示例 3:
输入:
s = “ab”
p = “."
输出: true
解释: ".” 表示可匹配零个或多个(‘*’)任意字符(‘.’)。
示例 4:
输入:
s = “aab”
p = “cab”
输出: true
解释: 因为 ‘*’ 表示零个或多个,这里 ‘c’ 为 0 个, ‘a’ 被重复一次。因此可以匹配字符串 “aab”。
示例 5:
输入:
s = “mississippi”
p = “misisp*.”
输出: false
对于这题使用递归或者非递归其实都可以,解题代码如下,可能我们需要考虑的细节比较难:
bool isMatch(char* s, char* p){
int ls = strlen(s), lp = strlen(p);
if (lp == 0) return (ls == 0 ? true : false); // 当p为空字符串
if (p[1] == '*') {
return isMatch(s, p + 2)|| (ls != 0 && (s[0] == p[0] || p[0] == '.') && isMatch(s + 1, p));
}
return ls != 0 && (s[0] == p[0] || p[0] == '.') && isMatch(s + 1, p + 1);
}
边栏推荐
- screen越看越好看
- 【科技、商業和管理】看劇學創業:《矽穀》第五季第4-6集
- JWT and session
- 面试题 10.03. 搜索旋转数组
- LeetCode_单调栈_中等_581.最短无序连续子数组
- Linear independence, orthogonal basis and orthogonal matrix
- Cypher usage statement record of neo4j
- Openstack explanation (XIV) -- Grace keystone registration
- NIO BIO AIO
- openstack详解(十六)——openstack Nova安装与数据库配置
猜你喜欢

MSF information collection based on TCP protocol

Understand the graph database neo4j (III)

机器学习笔记 - 探索 keras 数据集

【科技、商业和管理】看剧学创业:《硅谷》第五季第4-6集

【genius_platform软件平台开发】第三十六讲:内置数据类型的最大值宏定义

Openstack explanation (XV) -- basic principle of openstack Nova node

LeetCode_ Binary tree_ Prefix and_ Medium_ 437. path sum III

Openstack explanation (13) -- Grace keystone setup and startup

CVE-2019-0192 Apache solr远程反序列化代码执行漏洞危害
![[practical skills] inspiration from ai/ml of Google i/o 2022 Conference for developers](/img/1d/523177bc3b94aa0fef8d554e793e70.png)
[practical skills] inspiration from ai/ml of Google i/o 2022 Conference for developers
随机推荐
最长公共子序列和最长公共子串
C#入门系列(九) -- 方法使用
Cypher usage statement record of neo4j
Openstack explanation (XIV) -- Grace keystone registration
【科技、商业和管理】看剧学创业:《硅谷》第五季第4-6集
视觉SLAM总结——SuperPoint / SuperGlue
【图机器学习】启发式链路预测方法
【科技、商业和管理】看剧学创业:《硅谷》第六季第6-7集
机器学习笔记 - U-Net论文解读
【科技、商业和管理】看剧学创业:《硅谷》第六季第3-5集
[technology, business and management] drama learning and Entrepreneurship: Silicon Valley, episode 1-2, season 6
There is no network for the computer web browser, but QQ and wechat can log in to solve the browser network problem
【genius_platform软件平台开发】第三十五讲:UDP进行跨网段广播
[technology, business and management] drama watching and Entrepreneurship: Silicon Valley season 5 Episode 7-8
八、线性规划 顶点、极值点和基本可行解决方案
LeetCode_单调栈_中等_739. 每日温度
openstack详解(十七)——openstack Nova其他配置
[technology, business and management] drama learning and Entrepreneurship: Silicon Valley Season 6 Episode 3-5
机器学习笔记 - R语言学习入门系列一
MSF模块查找详解