【LeetCode】10.Regular Expression Matching(dp)
【题意】
给两个字符串s和p,判断s是否能用p进行匹配。
【题解】
dp[i][j]表示s的前i个是否能被p的前j个匹配。
首先可以分成3大类情况,我们先从简单的看起:
(1)s[i - 1] = p[j - 1],易得dp[i][j] = dp[i-1][j-1]
(2)p[i - 1] = '.',因为'.'可以匹配任何字符,所以dp[i][j] = dp[i-1][j-1]
(3)p[i - 1] = '*',这种情况就比较复杂了。
当p[j - 2] != s[i - 1] && p[j - 2] != '.'时,即当s = aba,p = abb*这种情况,因为 * 可以代表0个或多个,所以当p[j -2] != s[i - 1]时,往回倒退2个即dp[i][j] = dp[i][j - 2]
然后除去这种情况还有3种匹配情况,只要满足以下任意一种情况也是成立的
当 * 匹配多个时:dp[i][j] = dp[i - 1][j],即当s = abbb,p = ab*,因为b相等所以可以将s最后一个b删去,比较前面的abb与ab*从而实现 * 表示多个
当 * 匹配单个时:dp[i][j] = dp[i][j - 1],即当s = aba,p = aba*
当 * 匹配0个时:dp[i][j] = dp[i][j - 2],上面说过了
【代码】


1 class Solution {
2 public:
3 bool dp[35][35];
4 bool isMatch(string s, string p) {
5 s = " " + s;
6 p = " " + p;
7 memset(dp , false, sizeof(dp));
8 dp[0][0] = true;
9 int m = s.size(), n = p.size();
10 for (int i = 1; i <= m; i++){
11 for (int j = 1; j <= n; j++){
12 if (s[i - 1] == p[j - 1])dp[i][j] = dp[i - 1][j - 1];
13 else if (p[j - 1] == '.')dp[i][j] = dp[i - 1][j - 1];
14 else if (p[j - 1] == '*'){
15 if (s[i - 1] != p[j - 2] && p[j - 2] != '.')
16 dp[i][j] = dp[i][j - 2];
17 else{
18 dp[i][j] = dp[i][j - 1] || dp[i][j - 2] || dp[i - 1][j];
19 }
20 }
21 }
22 }
23 return dp[m][n];
24 }
25 };
【LeetCode】10.Regular Expression Matching(dp)的更多相关文章
- 【leetcode】10.Regular Expression Matching
题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- 【LeetCode】010. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ:Regular Expression Matching(正则表达式匹配)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- JVM终结篇
1.1 重新认知JVM 之前我们画过一张图,是从Class文件到类装载器,再到运行时数据区的过程.现在咱们把这张图不妨丰富完善一下,展示了JVM的大体物理结构图. 1.2 GC优化 内存被使用了之后, ...
- Linux 驱动框架---驱动中的异步
异步IO是对阻塞和轮询IO的机制补充,所谓异步IO就是在设备数据就绪时主动通知所属进程进行处理的机制.之所以说是异步是相对与被通知进程的,因为进程不知道也无法知道什么时候会被通知:这一机制非常类似于硬 ...
- μC/OS-III---I笔记5---多值信号量
多值信号量 操作系统中利用信号量解决进程间的同步和互斥(互斥信号量)的问题,在多道程序环境下,操作系统如何实现进程之间的同步和互斥显得极为重要.比如对同一部分资源的访问是要互斥,不能在另一个进程A在访 ...
- vue & this.$copyText
vue & this.$copyText click copy https://www.npmjs.com/package/vue-clipboard2 <p>{{message2 ...
- 如何使用 js 实现一个树组件
如何使用 js 实现一个树组件 tree component const arr = [ { id: 1, value: 1, level: 1, parentId: 0, }, { id: 2, v ...
- Web Performance API
Web Performance API 性能监测/性能优化 https://developer.mozilla.org/en-US/docs/Web/API/Performance https://d ...
- code magic
code magic CI/CD for mobile app projects https://codemagic.io https://codemagic.io/apps flutter http ...
- 微信公众号 & 付费阅读
微信公众号 & 付费阅读 付费功能 付费阅读 付费功能使用说明 1.付费功能介绍 开通了付费功能的公众号,运营者可以在编辑时对原创文章的部分或全部内容设置收费.对于付费图文,用户未付费前可免费 ...
- SDK & 埋点 & user behavior tracker
SDK & 埋点 & user behavior tracker 同一个 SDK ,根据不同的应用市场, 分别进行统计分析 ? https://www.umeng.com/ user ...
- vue的filter用法,检索内容
var app5 = new Vue({ el: '#app5', data: { shoppingList: [ "Milk", "Donuts", &quo ...