LeetCode-Regular Expression Matching
来源:互联网 发布:苹果电脑图片设计软件 编辑:程序博客网 时间:2024/05/18 22:12
这里写链接内容
这里写链接内容
这个是处理正则表达式的题目
核心问题是*的意思表示它前一位的重复次数可以为0
对于这类匹配 定义一个二维的DP数组,其中dp[i][j]表示s[0,i)和p[0,j)是否match,然后有下面三种情况
- P[i][j] = P[i - 1][j - 1], if p[j - 1] != ‘*’ && (s[i - 1] == p[j - 1] || p[j - 1] == ‘.’);
- P[i][j] = P[i][j - 2], if p[j - 1] == ‘*’ and the pattern repeats for 0 times;
- P[i][j] = P[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == ‘.’), if p[j - 1] == ‘*’ and the pattern repeats for at least 1 times.
class Solution{public: static const int FRONT=-1; bool isMatch(string s, string p) { int m = s.length(),n = p.length(); bool dp[m+1][n+1]; dp[0][0] = true;//初始化第0行,除了[0][0]全为false,毋庸置疑,因为空串p只能匹配空串,其他都无能匹配 for (int i = 1; i <= m; i++) dp[i][0] = false;//初始化第0列,只有X*能匹配空串,如果有*,它的真值一定和p[0][j-2]的相同(略过它之前的符号) for (int j = 1; j <= n; j++) dp[0][j] = j > 1 && '*' == p[j - 1] && dp[0][j - 2]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (p[j - 1] == '*') { dp[i][j] = dp[i][j - 2] || (s[i - 1] == p[j - 2] || p[j - 2] == '.') && dp[i - 1][j]; } else //只有当前字符完全匹配,才有资格传递dp[i-1][j-1] 真值 { dp[i][j] = (p[j - 1] == '.' || s[i - 1] == p[j - 1]) && dp[i - 1][j - 1]; } } } return dp[m][n]; }};
阅读全文
0 0
- LeetCode: Regular Expression Matching
- LeetCode: Regular Expression Matching
- leetcode Regular Expression Matching
- [Leetcode] Regular Expression Matching
- [LeetCode]Regular Expression Matching
- LeetCode-Regular Expression Matching
- Regular Expression Matching LeetCode
- LeetCode | Regular Expression Matching
- LeetCode: Regular Expression Matching
- Leetcode: Regular Expression Matching
- [LeetCode] Regular Expression matching
- [LeetCode] Regular Expression Matching
- LeetCode Regular Expression Matching
- leetcode Regular Expression Matching
- leetcode: Regular Expression Matching
- LeetCode Regular Expression Matching
- Leetcode: Regular Expression Matching
- leetcode:Regular Expression Matching
- throw和throws的区别
- python编程练习之三——堆栈和系统管理雏形
- O(∩_∩)O暑假总结
- 请求网络解析json串
- 关于Quartz任务不准时执行问题
- LeetCode-Regular Expression Matching
- 隐私政策
- 数据类型 数据类型转换 运算符
- WOJ1106-String's Puzzle
- BST树转换为有序双向循环链表
- 表达式求值基本构架
- BOM和DOM详解
- 1064: 加密字符
- OpenCV2编程手册笔记之 2.4迭代器遍历图像