当前位置:网站首页>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 上行下效,皇帝的新装
边栏推荐
- 【Hot100】34. 在排序数组中查找元素的第一个和最后一个位置
- Apicloud studio3 API management and debugging tutorial
- 函数传递参数小案例
- 山东大学暑期实训一20220620
- Overflow toolbar control in SAP ui5 view
- AVC1与H264的区别
- RHCSA10
- 碎片化知识管理工具Memos
- Binder通信过程及ServiceManager创建过程
- Yyds dry goods inventory # solve the real problem of famous enterprises: move the round table
猜你喜欢

关于 SAP UI5 floating footer 显示与否的单步调试以及使用 SAP UI5 的收益

Cf:a. the third three number problem

量价虽降,商业银行结构性存款为何受上市公司所偏爱?

Put functions in modules

Le rapport de recherche sur l'analyse matricielle de la Force des fournisseurs de RPA dans le secteur bancaire chinois en 2022 a été officiellement lancé.

Pycharm installation third party library diagram

MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!

Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications

Introduction to the principle of DNS

go 数组与切片
随机推荐
数据泄露怎么办?'华生·K'7招消灭安全威胁
mysql econnreset_Nodejs 套接字报错处理 Error: read ECONNRESET
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]
A specific example of ABAP type and EDM type mapping in SAP segw transaction code
Laravel document reading notes -mews/captcha use (verification code function)
Yyds dry goods inventory # solve the real problem of famous enterprises: move the round table
Lb10s-asemi rectifier bridge lb10s
将函数放在模块中
使用 jMeter 对 SAP Spartacus 进行并发性能测试
LeetCode20.有效的括号
155. 最小栈
峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系
Pandora IOT development board learning (HAL Library) - Experiment 7 window watchdog experiment (learning notes)
Write macro with word
RHCSA10
Halcon template matching actual code (I)
CAN和CAN FD
RHCSA2
函数传递参数小案例
一文详解ASCII码,Unicode与utf-8