当前位置:网站首页>leetcode 10. Regular Expression Matching 正则表达式匹配 (困难)
leetcode 10. Regular Expression Matching 正则表达式匹配 (困难)
2022-07-05 13:02:00 【InfoQ】
一、题目大意
- 1 <= s.length <= 20
- 1 <= p.length <= 30
- s 只包含从 a-z 的小写字母。
- p 只包含从 a-z 的小写字母,以及字符 . 和 *。
- 保证每次出现字符 * 时,前面都匹配到有效的字符
二、解题思路
三、解题方法
3.1 Java实现
public class Solution2 {
public boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
boolean[][] dp = new boolean[m + 1][n + 1];
dp[0][0] = true;
for (int i = 1; i < n + 1; i++) {
if (p.charAt(i - 1) == '*') {
// 星号 不会在第1个位置
dp[0][i] = dp[0][i - 2];
}
}
for (int i = 1; i < m + 1; i++) {
for (int j = 1; j < n + 1; j++) {
// 上个通配符可能是 点号、星号、字符
if (p.charAt(j - 1) == '.') {
// 上个通配符是 点号
dp[i][j] = dp[i - 1][j - 1];
} else if (p.charAt(j - 1) != '*') {
// 上个通配符不是 星号
dp[i][j] = dp[i - 1][j - 1] && p.charAt(j - 1) == s.charAt(i - 1);
} else if (p.charAt(j - 2) != s.charAt(i - 1) && p.charAt(j - 2) != '.') {
dp[i][j] = dp[i][j - 2];
} else {
dp[i][j] = dp[i][j - 1] || dp[i - 1][j] || dp[i][j - 2];
}
}
}
return dp[m][n];
}
}
四、总结小记
- 2022/7/5 上行下效,皇帝的新装
边栏推荐
- 峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系
- 精彩速递|腾讯云数据库6月刊
- MySQL 巨坑:update 更新慎用影响行数做判断!!!
- Yyds dry inventory JS intercept file suffix
- Concurrent performance test of SAP Spartacus with JMeter
- LeetCode20.有效的括号
- How can non-technical departments participate in Devops?
- 量价虽降,商业银行结构性存款为何受上市公司所偏爱?
- 【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
- RHCSA9
猜你喜欢

STM32 and motor development (from architecture diagram to documentation)

无密码身份验证如何保障用户隐私安全?

Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications

It's too convenient. You can complete the code release and approval by nailing it!

leetcode:221. 最大正方形【dp状态转移的精髓】

A deep long article on the simplification and acceleration of join operation

Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function

SAP SEGW 事物码里的 Association 建模方式

DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告

Changing JS code has no effect
随机推荐
It's too convenient. You can complete the code release and approval by nailing it!
【Hot100】34. 在排序数组中查找元素的第一个和最后一个位置
数据湖(七):Iceberg概念及回顾什么是数据湖
Halcon template matching actual code (I)
SAP SEGW 事物码里的 ABAP Editor
无密码身份验证如何保障用户隐私安全?
Yyds dry inventory JS intercept file suffix
Overflow toolbar control in SAP ui5 view
PyCharm安装第三方库图解
Pycharm installation third party library diagram
LB10S-ASEMI整流桥LB10S
946. Verify stack sequence
go map
OpenHarmony应用开发之Navigation组件详解
leetcode:221. Maximum square [essence of DP state transition]
How can non-technical departments participate in Devops?
Flutter InkWell & Ink组件
CAN和CAN FD
My colleague didn't understand selenium for half a month, so I figured it out for him in half an hour! Easily showed a wave of operations of climbing Taobao [easy to understand]
潘多拉 IOT 开发板学习(HAL 库)—— 实验7 窗口看门狗实验(学习笔记)