当前位置:网站首页>【LeetCode】10、正则表达式匹配
【LeetCode】10、正则表达式匹配
2022-06-24 13:02:00 【小曲同学呀】
10、正则表达式匹配
题目:
给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘*’ 的正则表达式匹配。
‘.’ 匹配任意单个字符
‘*’ 匹配零个或多个前面的那一个元素
所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。
示例 1:
输入:s = "aa", p = "a"
输出:false
解释:"a" 无法匹配 "aa" 整个字符串。
示例 2:
输入:s = "aa", p = "a*"
输出:true
解释:因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。
示例 3:
输入:s = "ab", p = ".*"
输出:true
解释:".*" 表示可匹配零个或多个('*')任意字符('.')。
提示:
1 <= s.length <= 20
1 <= p.length <= 30
s 只包含从 a-z 的小写字母。
p 只包含从 a-z 的小写字母,以及字符 . 和 *。
保证每次出现字符 * 时,前面都匹配到有效的字符
解题思路:
话说,这题目,真的,每个人理解的都会不一样。
比如:匹配零个或多个前面的那一个元素的意思 :可以理解为: * 和 *前(设为x)为一体的 ;或者 *前的元素和 * 分别是独立的。这两种理解,没毛病吧。
题目不难,但表达真的很拉跨。
抛开理解的问题,这道题用状态机转移是最好的解决方法。
代码如下:
人生苦短,我用java。
class Solution {
public boolean isMatch(String s, String p) {
return s.matches(p);
}
}

哈哈哈,好了,不闹了,言归正传。
正常代码:
class Solution {
public boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
boolean[][] f = new boolean[m + 1][n + 1];
f[0][0] = true;
for (int i = 0; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (p.charAt(j - 1) == '*') {
f[i][j] = f[i][j - 2];
if (matches(s, p, i, j - 1)) {
f[i][j] = f[i][j] || f[i - 1][j];
}
} else {
if (matches(s, p, i, j)) {
f[i][j] = f[i - 1][j - 1];
}
}
}
}
return f[m][n];
}
public boolean matches(String s, String p, int i, int j) {
if (i == 0) {
return false;
}
if (p.charAt(j - 1) == '.') {
return true;
}
return s.charAt(i - 1) == p.charAt(j - 1);
}
}
边栏推荐
- 杰理之检测 MIC 能量自动录音自动播放参考【篇】
- Antd checkbox, limit the selected quantity
- **Puzzling little problem in unity - light and sky box
- Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire
- 项目经理搭建团队,需要看6个特征
- leetcode:1504. 统计全 1 子矩形的个数
- SAP Marketing Cloud 功能概述(四)
- 2022年质量员-设备方向-岗位技能(质量员)复训题库及在线模拟考试
- AQS初探
- NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)
猜你喜欢

杰理之TIMER0 用默认的 PA13 来检测脉宽【篇】

2022 Quality Officer - Equipment direction - post skills (Quality Officer) recurrent training question bank and online simulation examination

Telecommuting: camping at home office gadgets | community essay solicitation

Gatling performance test

Puzzle (016.2) finger painting Galaxy

Rasa 3. X learning series - it is a great honor to be a source code contributor of Rasa contributors, and to build and share the rasa community with rasa source code contributors all over the world!

智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理

Research on MySQL composite index

Getting to know cloud native security for the first time: the best guarantee in the cloud Era

从谭浩强《C程序设计》上摘录的ASCII码表(常用字符与ASCII代码对照表)
随机推荐
百度地图API绘制点及提示信息
一个团队可以既做项目又做产品吗?
AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)
Antd checkbox, limit the selected quantity
杰理之开了多种解码格式插卡到播放等待的时间长【篇】
万用表测量电阻图解及使用注意事项
遠程辦公之:在家露營辦公小工具| 社區征文
Tupu software is the digital twin of offshore wind power, striving to be the first
在线文本实体抽取能力,助力应用解析海量文本数据
Autorf: learn the radiation field of 3D objects from single view (CVPR 2022)
2022质量员-设备方向-通用基础(质量员)考试题及答案
Research on MySQL composite index
【无标题】
[R language data science] (XIV): random variables and basic statistics
#21Set经典案例
Can a team do both projects and products?
杰理之检测 MIC 能量自动录音自动播放参考【篇】
English writing of Mathematics -- Basic Chinese and English vocabulary (common vocabulary of geometry and trigonometry)
How to manage tasks in the low code platform of the Internet of things?
OpenHarmony 1