当前位置:网站首页>【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);
}
}
边栏推荐
- Can a team do both projects and products?
- pip uninstall all packages except builtin package
- P2pdb white paper
- Promotion of Project Manager
- 2022煤矿瓦斯抽采操作证考试题及模拟考试
- 2022起重信号司索工(建筑特殊工种)复训题库及答案
- 居家办公更要高效-自动化办公完美提升摸鱼时间 | 社区征文
- The research on the report "market insight into China's database security capabilities, 2022" was officially launched
- Defoaming
- Jerry's test mic energy automatic recording automatic playback reference [article]
猜你喜欢

How to manage tasks in the low code platform of the Internet of things?

融云通信“三板斧”,“砍”到了银行的心坎上

初识云原生安全:云时代的最佳保障

Rongyun communication has "hacked" into the heart of the bank

21set classic case

unity 等高线创建方法

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

P2pdb white paper

居家办公更要高效-自动化办公完美提升摸鱼时间 | 社区征文

Gatling performance test
随机推荐
【无标题】
Jerry added an input capture channel [chapter]
How to solve the problem that iterative semi supervised training is difficult to implement in ASR training? RTC dev Meetup
如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]
**Puzzling little problem in unity - light and sky box
kotlin 异步流
NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)
Mit-6.824-lab4a-2022 (ten thousand words explanation - code construction)
MIT-6.824-lab4A-2022(万字讲解-代码构建)
OpenHarmony 1
P2pdb white paper
吉时利静电计宽测量范围
2022 recurrent training question bank and answers for hoisting signal Rigger (special type of construction work)
The research on the report "market insight into China's database security capabilities, 2022" was officially launched
Jupiter notebook operation
Jericho After sleep, the system will wake up regularly and continue to run without resetting [chapter]
Kotlin asynchronous flow
Redis面试题